Using automatic properties for explicit interface implementation was not possible in C# 5, but now that C# 6 supports getter-only auto-properties, this should be possible now, right?
Creating the auto-property succeeds in C# 6, but when trying to assign a value to it in the constructor, you have to cast this
to the interface type first, since the implementation is explicit. But that's where both VS 2015 RC and VS Code 0.3.0 display the error that can be seen in the comment:
using static System.Console;
namespace ConsoleApp
{
public interface IFoo { string TestFoo { get; } }
public class Impl : IFoo
{
// This was not possible before, but now works.
string IFoo.TestFoo { get; }
public Impl(string value)
{
// ERROR: Property or indexer 'IFoo.TestFoo' cannot be assigned to -- it is read only.
((IFoo)this).TestFoo = value;
}
}
public class Program
{
// Yes, not static. DNX supports that (for constructor DI).
public void Main(string[] args)
{
IFoo foo = new Impl("World");
WriteLine($"Hello {foo.TestFoo}");
ReadKey(true);
}
}
}
Note: I updated the original question that set a constant value to TestFoo
. In my real-world scenario, the value comes from an object that is injected into the constructor. The answer by Daniel A. White is excellent if the value returned by the property can be set at initialization.
It says:
Property or indexer 'IFoo.TestFoo' cannot be assigned to -- it is read only.
Is there a way around this, or do I still have to use properties with backing fields for this case?
I am using Visual Studio 2015 RC and Visual Studio Code 0.3.0 with DNX451 1.0.0-beta4.
I have raised an issue over at the Roslyn GitHub page.
The possible duplicate is a question about the definition of an interface with a regular property that can be read. My question is about implementing such an interface explicitly using a new C# 6 feature, that, in theory, should make this possible. See the other question I linked in the first sentence for a similar one (but for C# 5, where getter-only auto-properties where not available yet).