I'm building a WPF application in C#, and i have a question about the way getters and setters for class instances and their properties work; given the following code:
public class SomeClass
{
public int SomeProperty;
}
private SomeClass _anInstance;
public SomeClass anInstance
{
get{ return _anInstance; }
set
{
_anInstance = value;
}
}
anInstance = new SomeClass();
anInstance.SomeProperty = 5;
int anInt = anInstance.SomeProperty;
I'd expect this code to create at least one instance of the SomeClass, then to assign 5 to _anInstance.SomeProperty, and then set anInt to the value of _anInstance.SomeProperty but i'm not sure if it's using anInstance.SomeProperty or _anInstance.SomeProperty.
Which one is being assigned to 5/used to set anInt, and, for future reference, how would i be able to find out/know?
how would i be able to find out/know?
Set breakpoints and step into your code. The debugger will show you where the execution currently is.
Your public property anInstance
's getters and setters just point to the private _anInstance
field, and "redirect" reads and writes to that.
Since SomeClass is a Reference Type, anInstance is just a public reference to the same object _anInstance refers to. There is only one instance of SomeClass.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.