Skip to content

Properties

Mario Gutierrez edited this page Jan 7, 2017 · 1 revision

Properties are an implementation of the accessor/mutator (getter/setter) pattern.

A basic .NET property:

public int SomeInt
{
  get { return myInt; }
  set {  myInt = value; }
}

Auto-Properties

public int SomeInt { get; set; }

This uses a private field internally and sets that to a default value.

For ReferenceTypes, the default value is null; which can be a problem. In many cases, the default value will not be what you want. The solution to this is to assign a default value in the constructor. Alternatively, C# 6.0 includes a special syntax.

public string SomeString { get; set; } = "Default value.";
Clone this wiki locally