Accessing members in your own class: use (auto)properties or not?

前端 未结 6 1583
温柔的废话
温柔的废话 2021-01-22 21:16

I\'ve created this \"question\" as a community-wiki, because there is no right or wrong answer. I only would like to know how the community feels about this specific issue.

6条回答
  •  情歌与酒
    2021-01-22 22:09

    I think that there is no difference between these two approaches.

    Auto-implemented properties is just a quick way to access private members which are created any way.

    Example from MSDN:

    class Customer
    {
        // Auto-Impl Properties for trivial get and set
        public double TotalPurchases { get; set; }
        public string Name { get; set; }
        public int CustomerID { get; set; }
    
        // Constructor
        public Customer(double purchases, string name, int ID)
        {
            TotalPurchases = purchases;
            Name = name;
            CustomerID = ID;
        }
        // Methods
        public string GetContactInfo() {return "ContactInfo";}
        public string GetTransactionHistory() {return "History";}
    
        // .. Additional methods, events, etc.
    }
    

提交回复
热议问题