Difference in C# between different getter styles

前端 未结 3 1400
甜味超标
甜味超标 2020-12-05 01:55

I do sometimes see abbreviations in properties for the getter. E.g. those two types:

public int Number { get; } = 0

public int Number => 0;
3条回答
  •  抹茶落季
    2020-12-05 02:06

    Yes, both of them are read-only, but there is a difference. In the first one, there's a backing field which is initialized to 0 before the constructor is executed. You can change the value only in the constructor, just like a regular read-only field. The getter itself just returns the value of the field.

    In the second one, the getter just returns 0 every time, with no field involved.

    So to avoid using any automatically implemented properties or expression-bodied members at all, we have:

    First version

    private readonly int _number = 0;
    public int Number { get { return _number; } }
    

    Second version

    public int Number { get { return 0; } }
    

    A clearer example of the difference might be seen like this:

    public DateTime CreationTime { get; } = DateTime.UtcNow;
    public DateTime CurrentTime => DateTime.UtcNow;
    

    If you create a single object, its CreationTime property will always give the same result - because it's stored in a readonly field, initialized on object construction. However, every time you access the CurrentTime property, that will cause DateTime.UtcNow to be evaluated, so you'll get a potentially different result.

提交回复
热议问题