Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

297
Visualizações
Explicit implementation of an interface using a getter-only auto-property (C# 6 feature)

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).

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can get around this by using a read-only backing field for your explicitly implemented property. You can assign the injected value to the backing field within the constructor, the explicit property's get implementation will return it.

public class Impl : IFoo
{
    private readonly string _testFoo;

    string IFoo.TestFoo => _testFoo;

    public Impl(string value)
    {
        _testFoo = value;
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

I think you want this

string IFoo.TestFoo { get; } = "World";
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda