Properties – the basics

//Properties – the basics

Properties – the basics

By |2018-06-19T13:12:15+00:00March 4th, 2017|Categories: Technical|Tags: , |0 Comments

Properties in C# is a special type of methods called accessors, they are accessed as if they are public member fields but with read, write and compute the value.

Is is declared like a field but with a get and set blocks inside, get is to read the property value and must return the same property type, set is to write to the property, using an implicit parameter called value.

Typically properties read and write from/to fields,

private int _salary;
public int Salary
  get { return _salary; }
  set { _salary = value; }


but we can also declare properties without depending on a field “automatic properties – introduced in C# 3.0”, when an automatic property is use, the compiler generates a private field for this property.

public int SalaryAutomaticProperty { get; set; }


From MSDN:

A property is a member that provides a flexible mechanism to read, write, or compute the value of a private field. Properties can be used as if they are public data members, but they are actually special methods called accessors. This enables data to be accessed easily and still helps promote the safety and flexibility of methods.

Read Only Properties:

We can make a property read-only by not adding a set accessor to the property body:

public int ReadOnlySalary { get; }
private int quantity;
private int price;
public int ReadOnlyTotalPrice { get { return quantity * price; } }
myClass.ReadOnlyTotalPrice = 10; // compile error

We may also mark the set accessor as private, while this will prevent class consumers from changing the property value but this doesn’t mean that this property is a read only, it just means that the property set accessor is not accessible out side this class.

public int PrivateSetTotalPrice { get; private set; }

… And also note, that if we started to add a body for the get accessor, we will have to add a body for the set accessor as well, which may not make sense with read only purpose properties.

We can also make the property private field a readonly, which will make sure that the field will never change after initializing.


Starting from C# 6.0 we can declare a read only property as an expression-bodied property

public int ReadOnlyExpressionTotalPrice => quantity * price;

Which can contain more logic:

public string Config { get; } = string.IsNullOrWhiteSpace(
  string connectionString =
  connectionString : "<none>";


Initializing properties:

To initialize a property we can do this using the container class constructor or give the property’s private field value on deceleration.

Starting from C# 6.0 we can initialize the property inline like this:

public int InlineInitProperty { get; set; } = 10;

and it can be used on read only properties as well

public int ReadOnlyInlineInitProperty { get; } = 10;


Finally, It is highly recommended to read “Why Properties Matter“!


examples used

About the Author:

Senior Software Engineer.

Leave A Comment