Difference in C# between different getter styles

WoIIe

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

public int Number { get; } = 0

public int Number => 0;

Can someone please tell me if there are any differences between those two. How do they behave? Are both of them read-only?

Jon Skeet

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Switching quickly between different C styles in emacs

From Dev

difference between jquery styles

From Dev

Difference between different types of Instantiation in c#

From Dev

What is the difference/advantages between these two declaration styles

From Dev

Difference between IsMouseOver and IsHighlighted in WPF styles

From Dev

Android styles, difference between api levels

From Dev

React Native - difference between parameters in tag and styles

From Dev

What is the difference between two different type with same size in C?

From Dev

Difference between different ways of malloc

From Dev

Difference between different scanf formats

From Dev

Difference between different scanf formats

From Dev

Is there a difference between having a private setter OR only defining a getter?

From Dev

difference between a getter method and a method which returns the state of an instance variable?

From Dev

Difference between Getter/Setter and this other Data object Class

From Dev

What is the difference between sightly var and calling of sling model's getter?

From Dev

Difference between using getter and setter methods to add list to a bean property?

From Dev

What is the difference between using @XmlElement before field and before getter declaration?

From Dev

Are these two styles of C function pointer definition different?

From Dev

Is there any practical difference between direct formatting and character styles in Microsoft Word?

From Dev

What's the difference between below two code styles

From Dev

What is the difference between the two styles of documentation comments in Rust? (/// vs //!)

From Dev

Difference between // and /// in c#

From Dev

Difference between += and =+ in C++

From Dev

Difference between ! and ~ in c#

From Dev

Difference between }; and } in C++

From Dev

Difference between "* " , " * " and " *" pointers in C

From Dev

the difference between [,] and [][] in C#

From Dev

Why is there a difference between using c++ string+string temporary object by different compilers?

From Dev

Two different pointer syntax in C which one is right and what's the difference between the two?

Related Related

  1. 1

    Switching quickly between different C styles in emacs

  2. 2

    difference between jquery styles

  3. 3

    Difference between different types of Instantiation in c#

  4. 4

    What is the difference/advantages between these two declaration styles

  5. 5

    Difference between IsMouseOver and IsHighlighted in WPF styles

  6. 6

    Android styles, difference between api levels

  7. 7

    React Native - difference between parameters in tag and styles

  8. 8

    What is the difference between two different type with same size in C?

  9. 9

    Difference between different ways of malloc

  10. 10

    Difference between different scanf formats

  11. 11

    Difference between different scanf formats

  12. 12

    Is there a difference between having a private setter OR only defining a getter?

  13. 13

    difference between a getter method and a method which returns the state of an instance variable?

  14. 14

    Difference between Getter/Setter and this other Data object Class

  15. 15

    What is the difference between sightly var and calling of sling model's getter?

  16. 16

    Difference between using getter and setter methods to add list to a bean property?

  17. 17

    What is the difference between using @XmlElement before field and before getter declaration?

  18. 18

    Are these two styles of C function pointer definition different?

  19. 19

    Is there any practical difference between direct formatting and character styles in Microsoft Word?

  20. 20

    What's the difference between below two code styles

  21. 21

    What is the difference between the two styles of documentation comments in Rust? (/// vs //!)

  22. 22

    Difference between // and /// in c#

  23. 23

    Difference between += and =+ in C++

  24. 24

    Difference between ! and ~ in c#

  25. 25

    Difference between }; and } in C++

  26. 26

    Difference between "* " , " * " and " *" pointers in C

  27. 27

    the difference between [,] and [][] in C#

  28. 28

    Why is there a difference between using c++ string+string temporary object by different compilers?

  29. 29

    Two different pointer syntax in C which one is right and what's the difference between the two?

HotTag

Archive