Hi,
Say if I have the following code snippet:-
Class Class1
{
public string fullName { get; set; }
public string FullName { get; set; }
public string ToString()
{
fullname = this.FullName;
if (fullname == null)
{
return base.ToString();
}
return fullname;
}
}
When the ToString method is called, is it the default constructor that includes and initializes the 2 properties, as obviously these fall out of scope\range of the method implementation?
I know that fields are included in the default constructor, but does this apply to properties also?
Regards,
Loading
VulpesPosted Apr 9, 2011, 4:52 PM
So here the hidden fields are null initially and the properties therefore return nulll as well.
There's no way to access this hidden field from C# which is given a compiler-generated name.
VulpesPosted Apr 10, 2011, 5:52 AM
Sam HobbsPosted Apr 10, 2011, 12:26 AM
I think a property should be considered a method, certainly to the extent that a constructor is not relevant in this context. The sample code that you show happens to work a little differently, and I would not be able to explain things as accurately as Vulpes has. I think however that the sample code might cause confusion since it is not normal for memory to be allocated for data corresponding to a property.