Search This Blog

Monday, 27 April 2015

C# 6.0 New Features : Property Initializers

Let's go in flashback and try to understand how properties evolved in .Net.

Properties are wrapper over fields which controls the way our fields should be accessed and modified.
A typical field:
private int i = 0;

By default variable in a class are private which means they cannot be accessed outside the classes and are only accessible via member functions.

Making the variable public will mean anybody who has access to class object can modify the variable, which is not a secure way of dealing with class data.

Solution is properties:

A typical .Net 1.1 property

private int _i = 0;

public int I
{
get { return _i; }
 set { _i = value; }
 }

or we can use access modifiers with properties like

public int I
{
get { return _i; }
protected set { _i = value; }
 }

Above code means property is available via class objects but only child classes can set.

Come .Net  2.0 Come Automatic Properties:

.Net 2.0 bring about the concept of automatic properties which removed the reliance on creating a field to be encapsulated by a property

public int I {get; set; }

Here compiler created a backing field automatically which is not accessible to code but used internally.

Still the gap was the properties can't have a default value other than default value of type provided by the framework i.e. if i want to initialize property "I" decalred above with value 5, I need to do that in constructor e.g

class A
{
public int I {get; set; }

public A()
{
 I =5;
}

}

.Net 6.0 removed this reliance of setting default properties in constructor by introducing Property Initializer, which are a concise way of providing default values as

class A
{
public int I {get; set; } = 5;

public A()
{
  // I =5;  //No longer needed now
}

}


No comments:

Post a Comment