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

231
Visualizações
Property value of "base" in debugger

I'm debugging the following code:

class A 
{
    public virtual string X => "A";
}

class B : A
{
    public bool OwnX { get; set; } = true;
    public override string X
        => OwnX ? "B" : base.X; // (o)
}

class Program
{
    static void Main() => Console.WriteLine(new B().X);
}

And I have a breakpoint on the line marked with (o). When the breakpoint hit, I'm trying to evaluate base.X and getting its value "B":

strage base.X value?

The question is: why not "A"?

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

0

This is a Roslyn bug

As others have mentioned, this bug is well known.

You can trivially check that the actual value of base.X is A, it is just the Expression Evaluator that returns the wrong result:

This one is in Rider

over 4 years ago · Santiago Trujillo Relatório

0

Because X is not evaluated in runtime, it will also not evaluate in the debugger. So it assumes it is the same.

Because base.X is not actually called, it has never been evaluated as A. The overwriting property is because of that leading.

If you'd like to do so, make a constant out of it.

over 4 years ago · Santiago Trujillo Relatório

0

Maybe you should read a little bit more about this ?: Operator https://docs.microsoft.com/en-us/dotnet/articles/csharp/language-reference/operators/conditional-operator

Since default value OwnX is true.

Give it a try to this code:

 B b = new B();
 b.OwnX = false;
 Console.WriteLine(b.X);

Explaining this code: public override string X => OwnX ? "B" : base.X;

to make it more readable:

public override string X
{
   get {
      if (Ownx == true) // this is the default value.
      {
          return "B";
      }
      else{
          return "A";
      }
}
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