The first snippet is a correct auto-property with an initializer:
Auto-Implemented Properties - C# Programming Guide | Microsoft Docs[
^]
Auto properties have been around since C# 3; the property initializer syntax was added in C# 6.
The second snippet is a
StackOverflowException
waiting to happen. The property
get
method calls itself in an infinite recursion. The property
set
method does the same.
The correct implementation requires a field:
private List<Pizza> _pizzas = new List<Pizza>();
public List<Pizza> Pizzas
{
get { return _pizzas; }
set { _pizzas = value; }
}
As you can see, that's a lot more code "ceremony" for exactly the same result.