There is no (explicit) reference to a firstName private variable which FirstName is supposed to be hiding. Could you explain how this works? I assume there is some private variable that is being getted and setted. Thanks.
// auto-implemented property FirstName
public string FirstName { get; set; }
Basically the compiler converts your code into something like this:
private string <__>firstName;
public string FirstName
{
get { return <__>firstName; }
set { <__>firstName = value; }
}
That's unlikely to be the exact name, but the use of angle brackets in the name is important - because it makes it an unspeakable name. (That's unofficial terminology, but widely used - I don't know whether Eric Lippert actually coined it, or whether he was just the first person to use it in something I read.) It's a name which isn't a valid C# identifier, but which the CLR is quite happy with. That has two benefits:
It uses the same technique for all kinds of other generated code - anonymous types, anonymous functions, iterator blocks etc.
yes,
the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors.
(c) msdn
EDIT:
When you define a property, compiler will emit 2 methods: get_XXX
and set_XXX
. When the C# compiler sees code that's trying to get or set a property, the compiler actually emits a call to one of these methods. (c) "CLR via C#"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With