0

I need a sanity check to determine whether my C# compiler is broken or I'm not understanding how automatic properties are supposed to work.

Given the following class definition and constructor, note the respective member values.

public class MyClass
{
    public string MyString { get; set; }
    public string _anotherString;

    public MyClass()
    {
        MyString = "some value";
        // <--- debugger shows MyString as null

        _anotherString = "another value";
        // <--- debugger shows _anotherString as "another value"
    }
}

This continues to happen with a variety of classes. In short, all automatic properties fail to initialize when assigning values in the constructor. They work everywhere else. However, if I change them to use backing stores and initialize the backing variable, they are initialized properly.

Am I correct in assuming that it is always safe to initialize automatic properties in the constructor, or am I missing something? And if it is, then what could be going on with Visual Studio?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
John Holliday
  • 1,268
  • 1
  • 11
  • 19
  • 2
    Do you break for the first time *at* the line `MyString = ...` or at the next line? Your comment suggests that on the next line, but you cannot have a breakpoint on a comment. – BartoszKP Oct 13 '13 at 00:14
  • 2
    Can you display the output aside from using the debugger? i.e. `Console.WriteLine` or `MessageBox.Show(...)`, etc. Needless to say, automatic properties do not behave in the way you describe. – Kirk Woll Oct 13 '13 at 00:19
  • Check with `Console.WriteLine()`. I think there are some bugs in the debugger that result in incorrect debugger displays. – siride Oct 13 '13 at 00:20
  • 2
    @siride: Who debugs the debugger ...? – CSJ Oct 13 '13 at 00:20
  • 1
    @CSJ that's one of those terrifying questions I like to avoid thinking about ;). – siride Oct 13 '13 at 00:21
  • @BartoszKP, I've omitted the code between the two assignments. The breakpoint is immediately after each assignment. – John Holliday Oct 13 '13 at 00:32
  • 1
    This is strange. As noted by @KirkWoll the behaviour you describe is not what it should be. I'd guess some problems with outdated pdb or configuration issues. – BartoszKP Oct 13 '13 at 00:34
  • @Kirk, the output is the same for Console.WriteLine, etc. In fact, after exiting the constructor, the value of MyString is still null. – John Holliday Oct 13 '13 at 00:34

2 Answers2

0

Have you tried with a fresh new project? Did you CLEAN the solution (Build > Clean Solution), close all opened documents/tabs and then do a rebuild (not a build, rebuild!) and try again?

Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
-2

I determined that this was caused by a conflict between two PostSharp aspect attributes. Not a problem with PostSharp, but with a custom attribute I had created to intercept property assignments. Removing the extra attribute allowed it to work normally.

Essentially, when there are multiple aspects applied to the same object, PostSharp does some tricky maneuvers when "weaving" the generated code. Still trying to determine how to fix it, but at least I'm not going completely insane.

John Holliday
  • 1,268
  • 1
  • 11
  • 19
  • 3
    Given that you had PostSharp aspect attributes on property assignments, chances are you should have _said so_ in your question. – John Saunders Oct 13 '13 at 01:38
  • @JohnSaunders - I thought the phrase "sanity check" was sufficient to indicate my need for clarification on the C# language and the way automatic properties are supposed to work. The quick responses actually helped to validate my understanding and forced me to dig deeper to find the root of the problem, which was buried in a separate assembly. In my humble opinion, the value of being connected to a network of experts like yourself is being able to ask questions like these when things just don't make sense. – John Holliday Oct 13 '13 at 15:02
  • This particular "expert" feels you wasted my time by not telling me that PostSharp is involved. The fact that PostSharp is being used changes this from being about the C# language to being about an arbitrary language you made up yourself. – John Saunders Oct 13 '13 at 16:31
  • That's fair, John. My apologies. – John Holliday Oct 13 '13 at 20:25