-1

Failing finding an answer I can understand, could someone explain the difference between the following:

// One
private Guid mCategoryID;
public Guid CategoryID
{
    get
    {
        return mCategoryID;
    }
    set
    {
        mCategoryID = value;
    }
}
    
// Two
public Guid CategoryID {get; set;}

Is the get/set code in the section above: One doing the same as Two or do they work differently?

Jerry

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
  • 1
    See here https://stackoverflow.com/questions/1180860/public-fields-versus-automatic-properties – Niklas Jul 15 '20 at 07:12
  • 3
    Does [this](https://stackoverflow.com/questions/6001917/what-are-automatic-properties-in-c-sharp-and-what-is-their-purpose) answer your question? – Sweeper Jul 15 '20 at 07:12
  • 1
    Does this answer your question? [Public Fields versus Automatic Properties](https://stackoverflow.com/questions/1180860/public-fields-versus-automatic-properties) – Niklas Jul 15 '20 at 07:12
  • Sweeper's answers provides it quite simply: There is none, it's [syntactic sugar](https://en.wikipedia.org/wiki/Syntactic_sugar) – MindSwipe Jul 15 '20 at 07:14
  • As other have already said, its syntactic sugar indeed. But it also avoids the risk of linking the property to the wrong private variable... I would encourage you to use automatic properties. – Cleptus Jul 15 '20 at 07:31

1 Answers1

0

Yes, Both one and two are same .

  • A property which contains a both get and set accessors, then we will call it as read-write property.
  • get accessor, then we will call it as a read-only property. -set accessor, then we will call it as write-only property.
Bikalpkarn
  • 11
  • 5