1

In C# 6 you can have the following property:

public Uri MyProperty => new Uri();

Or you can have this:

public Uri MyProperty1 { get; } = new Uri();

Whats the difference between them?

TheLethalCoder
  • 6,668
  • 6
  • 34
  • 69
Zeus82
  • 6,065
  • 9
  • 53
  • 77

2 Answers2

2

The first returns a new Uri object each time you access the property - the second initializes the property to a new Uri object and gives the same object every time.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
0

The difference is that this will create a new instance of Uri every time:

public Uri MyProperty => new Uri();

And this will work with backing field with assigned value:

public Uri MyProperty1 { get; } = new Uri();
Fabjan
  • 13,506
  • 4
  • 25
  • 52