Question : C# Getter Setter Question?

I've seen code that uses something like this: objectx.set_Name and the object's class has a property Name that is public.  I would have used objectx.Name - Is the objectx.set_Name valid in c# and why is it there?  Is there some benefit to using this or any reason it should be used in favor of my objectx.Name version?  I just have never read about this type of getter/setter syntax.  What is this syntax called?   Is the _ the same as the dot?
Thanks.

Answer : C# Getter Setter Question?


get_Name and set_Name like methods are internally added by the c# compiler to the class when you define a property Name. For a property X having get; and set; c# compiler will add get_X and set_X methods.

Property is only a higher level feature and internally a class can have only fields and methods (atleast in this conext), so the compiler maps the properties with methods.

There is no reason you have to use it directly. You can just use the property.
When you use a property, during compilation, this line => objectx.Name
is coverted to objectx.get_Name.
So there is no property concept at the lower level.
Random Solutions  
 
programming4us programming4us