Why use private members then use public properties to set them?

后端 未结 5 1525
故里飘歌
故里飘歌 2020-12-11 00:53

Seen a few examples of code where this happens:

public class Foo
{
    string[] m_workID;
    public string[] WorkID
    {
        get
        {
                     


        
5条回答
  •  抹茶落季
    2020-12-11 01:05

    You can use C# 3.0 auto properties feature to save time typing:

    public class Foo
    {
        public string[] WorkID
        {
            get; private set;
        }
    }
    

    In addition properties gives you lot of advantages in comparison to fields:

    • properties can be virtual

    • properties hide implementation details (not all properties are just trivial variable accessors)

    • properties can contain validation and logging code and raise change events

    • interfaces cannot contains fields but properties

提交回复
热议问题