What is the difference between a Field and a Property in C#?

Answer

Properties expose fields. Fields should (almost always) be kept private to a class and accessed via get and set properties. Properties provide a level of abstraction allowing you to change the fields while not affecting the external way they are accessed by the things that use your class.

publicclassMyClass{// this is a field.  It is private to your class and stores the actual data.privatestring _myField;// this is a property.  When you access it uses the underlying field, but only exposes// the contract that will not be affected by the underlying fieldpublicstringMyField{
        get
        {return _myField;}set{
            _myField = value;}}}

All c sharp Questions

Ask your interview questions on c-sharp

Write Your comment or Questions if you want the answers on c-sharp from c-sharp Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---