User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2018 - First Crazy Developer (Abhishek Kumar)

crazydeveloper New features in C# 6 That we should know Part-1

Hi friend,
After discussion of lots of features in C#, today we will discuss about C# 6.0 new features.

Some days before I installed Visual Studio 2015 in my system. It introduces a completely new version of ASP.Net with include C# 6 language. In previous version they introduced lots of new concept like async and await in version 5, dynamic in version 4, LINQ in version 3 and so on. Now when we talk about C# 6, there are no any major feature changes came with this version, but there are a lots of syntactical improvements which will help you for better coding styles.

1. Primary Constructor &  Auto Implemented Properties Initializers:-

Today we will discuss about Primary Constructor & Auto Implemented Properties Initializers. Primary Constructor feature of C# 6 announced with Visual Studio 2014, which help destroy the pain of capturing values of constructor parameters to fields in the class or structure for further operations. The main use of this functionality to use constructor parameters in the class definition without a formal constructor declaration. But most interesting part which I would like to say that now it has been removed from Visual Studio 2015.

Look the following code:-

  1. //this is the primary constructor:
  2. public class Square(double side)
  3. {
  4. public double Side{get;set;}
  5. }
  6. public class Student(string firstName, string lastName)
  7. {
  8. public Student(string fullName):this(firstName+' '+lastName)
  9. {
  10. }
  11. public string FirstName { get; set; } = firstName;
  12. public string LastName { get; } = lastName;
  13. }

In above code is example of primary constructor. In above syntax we passed a parameter with class declaration. Once you have a primary constructor, all other constructors must ultimately call into the primary constructor using this (), which makes sense since we always want the primary constructor arguments to be available for initialization (although a struct will still have a non- replaceable default constructor that will initialize all members to default values).

Now after the above code now come to the point, on November 12, 2014 Microsoft announced  the release of Visual Studio 2015 on the day of Visual Studio Connect() from New York, USA where they announced many new features as well as enhancements with C# 6.0.  So we should know that after this release above functionality in not applicable but we have a new functionality which can use place of above requirement.

If we want to set a default value for a property in a class, previously we can do it with primary constructor but in latest version in C# 6 introduces 'Auto-property initializers'. Through which we can assign default value for a property as part of the property declaration.

  1. public class Student
  2. {
  3. public int RollNumber { get; set; }
  4. public string FirstName { get; set; } = string.Empty;
  5. public string LastName { get; set; } = string.Empty;
  6. public string FullName { get { return string.Format("{0] {1}", FirstName, LastName); } }
  7. public DateTime AdmissionDate { get; protected set; } = DateTime.UtcNow;
  8. public DateTime DateofBirth { get; set; }
  9. public ICollection Qualifications { get; set; } = new HashSet<Qualification>();
  10. }

If we go through above code, we get a new style of syntax. In above code we can directs set the values with properties. This types of syntax called 'Auto-property initializers'.

2. Expression Bodied members (Functions and Properties):-

In above code the FullName property in the Student class is read only. This is basic important things, because sometime interviwer ask, how can we defined read only property in C#? The value of this property intialize from an expression in the get accessor. But in C# 6 we can do this with the simplified code like:

  1. public string FullName => string.Format("{0} {1}", FirstName, LastName);

The expression bodied simplification isn’t limited to property, we can use with function also. Expression bodied functions are another syntax simplification in C# 6.0.

Look the following code:

  1. public TimeSpan GetStudentAge() => DateTime.Now - DateofBirth;

These are functions with no statement body. Instead, you implement them with an expression following the function declaration. The return type of the expression must, of course, match the return type identified in the function declaration

3. Getter-only auto-properties:-

When we defined any properties in C# 5 and lower version, we must provide a get and set. If we want the property value to be immutable, we used the private accessor on the setter. But in C# 6, we can now omit the set accessor to achieve true readonly auto implemented properties:

Look the following code:

  1. public DateTime DateofBirth { get; }
  2. public string ConnectionString { get; } = readConnectionStringFromConfig();

Apart from this blog post, I hope we will get chance to use this new feature and enjoy  with C# 6 new features.

crazydeveloper Home Page 10 July 2015

Become a Fan