is it valid to use auto implemented private members created by properties? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

is it valid to use auto implemented private members created by properties?

"to create a private member that can only be accessed through the Name property's get and set accessors, use the following syntax: public string Name { get; set; } As you can see, you do not need to declare the private field name separately - it is created by the property automatically. Name is called an auto-implemented property. Also called auto-properties, they allow for easy and short declaration of private members. " if the private field is already implemented then it's valid to use in the same class , however it's not valid when i try it , so what would be the name of the private field and how to use it without the property ??

19th Dec 2017, 11:15 AM
Hadeer Mohamed
Hadeer Mohamed - avatar
1 Answer
0
If you use a auto property, then you must use properties to access the field, in this example you'll use "Name", otherwise you'll want to do a manual implementation ie: private string mName; public string Name { get { return mName; } set { mName = value; } } Then in the declaration class, you can use mName (or anything else you want to name the private field to) to access field and use Name in other classes as the property.
19th Dec 2017, 11:24 AM
Hassie
Hassie - avatar