Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!