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

前端 未结 12 1342
一整个雨季
一整个雨季 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:04

    Pre-C# 6

    I'd use the last of these, for a trivial property. Note that I'd call this a public property as both the getters and setters are public.

    Immutability is a bit of a pain with automatically implemented properties - you can't write an auto-property which only has a getter; the closest you can come is:

    public string Foo { get; private set; }
    

    which isn't really immutable... just immutable outside your class. So you may wish to use a real read-only property instead:

    private readonly string foo;
    public string Foo { get { return foo; } }
    

    You definitely don't want to write getName() and setName(). In some cases it makes sense to write Get/Set methods rather than using properties, particularly if they could be expensive and you wish to emphasize that. However, you'd want to follow the .NET naming convention of PascalCase for methods, and you wouldn't want a trivial property like this to be implemented with normal methods anyway - a property is much more idiomatic here.

    C# 6

    Hooray, we finally have proper read-only automatically implemented properties:

    // This can only be assigned to within the constructor
    public string Foo { get; }
    

    Likewise for read-only properties which do need to do some work, you can use member-bodied properties:

    public double Area => height * width;
    
    0 讨论(0)
  • 2020-12-02 07:07

    As mentioned, all of these approaches result in the same outcome. The most important thing is that you pick a convention and stick with it. I prefer using the last two property examples.

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

    Whenever possible I prefer public string Name { get; set; } as it's terse and easily readable. However, there may be times when this one is necessary

    private string name;
    
    public string Name {
       get { return name; }
       set { name = value; }
    }
    
    0 讨论(0)
  • 2020-12-02 07:11

    In C# the preferred way is through properties rather than getX() and setX() methods. Also, note that C# does not require that properties have both a get and a set - you can have get-only properties and set-only properties.

    public boolean MyProperty
    {
        get { return something; }
    }
    
    public boolean MyProperty
    {
        set { this.something = value; }
    }
    
    0 讨论(0)
  • 2020-12-02 07:11

    Which one should I use, and what are the caveats or subtleties involved with each approach?

    When going with properties there is one caveat that has not been mentioned yet: With properties you cannot have any parametrization of your getters or setters.

    For example imagine you want to retrieve a list items and want to also apply a filter at the same time. With a get-method you could write something like:

    obj.getItems(filter);
    

    In contrast, with a property you are forced to first return all items

    obj.items
    

    and then apply the filter in the next step or you have to add dedicated properties that expose items filtered by different criteria, which soon bloats your API:

    obj.itemsFilteredByX
    obj.itemsFilteredByY
    

    What sometimes can be a nuisance is when you started with a property, e.g. obj.items and then later discovered that getter- or setter-parametrization is needed or would make things easier for the class-API user. You would now need to either rewrite your API and modify all those places in your code that access this property or find an alternative solution. In contrast, with a get-method, e.g. obj.getItems(), you can simply extend your method's signature to accept an optional "configuration" object e.g. obj.getItems(options) without having to rewrite all those places that call your method.

    That being said, (auto-implemented) properties in C# are still very useful shortcuts (for the various reasons mentioned here) since most of the time parametrization may not be needed – but this caveat stands.

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

    Regardless of which way you choose in C# the end result is the same. You will get a backinng variable with separate getter and setter methods. By using properties you are following best practices and so it's a matter of how verbose you want to get.

    Personally I would choose auto-properties, the last version: public string Name { get; set; }, since they take up the least amount of space. And you can always expand these in the future if you need add something like validation.

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