Click here to Skip to main content
14,244,116 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have a project that does not seem to behave properly when using auto implemented properties.

When using get/set, it returns a null. But when explicitly telling it what to return it works great. For example, I only need to change this:
public string Symbol { get; set; }

to this:
public string Symbol
    get { return _symbol; }
    set { _symbol = value; }

And it works great.

Does anyone have an explanation?
Updated 3-Dec-10 4:06am
TheyCallMeMrJames 15-Nov-10 11:42am
make sure you comment on people's answers (don't add an answer yourself) or they won't get an email with your notes. cheers.
wizardzz 15-Nov-10 12:05pm
Thank you very much, Mr. James, I just noticed that you corrected this for me.
TheyCallMeMrJames 15-Nov-10 15:29pm
no worries. first one's free but after that it's a million bucks a pop. ;o)
Rate this:
Please Sign up or sign in to vote.

Solution 4

Do you have string _symbol defined? If you do, remove it and see if it works. You might have a collision there (though, that doesn't explain if you're actually getting null).

Basically, just make sure if you're using the shortcut that you're not also declaring the private member.

wizardzz 15-Nov-10 11:51am
You got it, I did not think that this would make a difference to the compiler. I had to change the constructor of this class to use Symbol instead of _symbol now.
Rate this:
Please Sign up or sign in to vote.

Solution 1

No explanation but I just tried it and I had no problem with the auto implemented version.
TheyCallMeMrJames 15-Nov-10 11:41am
from OP: Thank you for trying it out Henry.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Maybe it helps to use Reflector[^] and take a look at the generated code when being compiled as an automatic property?

TheyCallMeMrJames 15-Nov-10 11:42am
from OP: Uwe, I used reflector and the automatic property code looks identical to the code generated by the more explicit method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100