Getters, setters, and properties best practices. Java vs. C#

前端 未结 12 1343
一整个雨季
一整个雨季 2020-12-02 06:32

I\'m taking a C# class right now and I\'m trying to find out the best way of doing things. I come from a Java background and so I\'m only familiar with Java best-practices;

相关标签:
12条回答
  • 2020-12-02 07:13

    like most of the answers here, use Automatic properties. Intuitive, less lines of code and it is more clean. If you should serialize your class, mark the class [Serializable]/ with [DataConract] attribute. And if you are using [DataContract] mark the member with

    [DataMember(Name="aMoreFriendlyName")]
    public string Name { get; set; }
    

    Private or public setter depends on your preference.

    Also note that automatic properties require both getters and setters(public or private).

    /*this is invalid*/
    public string Name 
    { 
        get; 
       /* setter omitted to prove the point*/
    }
    

    Alternatively, if you only want get/set, create a backing field yourself

    0 讨论(0)
  • 2020-12-02 07:14
    public string Name { get; set; }
    

    This is simply a auto-implemented property, and is technically the same as a normal property. A backing field will be created when compiling.

    All properties are eventually converted to functions, so the actual compiled implementation in the end is the same as you are used to in Java.

    Use auto-implemented properties when you don't have to do specific operations on the backing field. Use a ordinary property otherwise. Use get and set functions when the operation has side effects or is computationally expensive, use properties otherwise.

    0 讨论(0)
  • 2020-12-02 07:23

    First let me try to explain what you wrote:

    // private member -- not a property
    private string name;
    
    /// public method -- not a property
    public void setName(string name) {
       this.name = name;
    }
    
    /// public method -- not a property
    public string getName() {
       return this.name;
    }
    
    // yes it is property structure before .Net 3.0
    private string name;
    public string Name {
       get { return name; }
       set { name = value; }
    }
    

    This structure is also used nowadays but it is most suitable if you want to do some extra functionality, for instance when a value is set you can it to parse to capitalize it and save it in private member for alter internal use.

    With .net framework 3.0

    // this style is introduced, which is more common, and suppose to be best
    public string Name { get; set; }
    
    //You can more customize it
    public string Name
    {
        get;
        private set;    // means value could be set internally, and accessed through out
    }
    

    Wish you better luck in C#

    0 讨论(0)
  • 2020-12-02 07:24

    In C# favor properties for exposing private fields for get and/or set. The thie form you mention is an autoproperty where the get and set automatically generate a hidden pivot backing field for you.

    I favor auto properties when possible but you should never do a set/get method pair in C#.

    0 讨论(0)
  • 2020-12-02 07:25

    Use properties in C#, not get/set methods. They are there for your convenience and it is idiomatic.

    As for your two C# examples, one is simply syntactic sugar for the other. Use the auto property if all you need is a simple wrapper around an instance variable, use the full version when you need to add logic in the getter and/or setter.

    0 讨论(0)
  • 2020-12-02 07:31

    If all you need is a variable to store some data:

    public string Name { get; set; }
    

    Want to make it appear read-only?

    public string Name { get; private set; }
    

    Or even better...

    private readonly string _name;
    
    ...
    
    public string Name { get { return _name; } }
    

    Want to do some value checking before assigning the property?

    public string Name 
    {
       get { return m_name; }
       set
       {
          if (value == null)
             throw new ArgumentNullException("value");
    
          m_name = value;
       }
    }
    

    In general, the GetXyz() and SetXyz() are only used in certain cases, and you just have to use your gut on when it feels right. In general, I would say that I expect most get/set properties to not contain a lot of logic and have very few, if any, unexpected side effects. If reading a property value requires invoking a service or getting input from a user in order to build the object that I'm requesting, then I would wrap it into a method, and call it something like BuildXyz(), rather than GetXyz().

    0 讨论(0)
提交回复
热议问题