Home>

The content of this article includes the new language features introduced in c#6.0. There is also a new compiler named "roslyn" that has been introduced. The compiler is open source,And you can download the source code from this address of the codeplex website:https://roslyn.codeplex.com/.

New features in c#6.0

We can discuss these new features one by one,And first a list of these features in c#6.0

Auto property initializer

Primary consturctor

Dictionary initializer

Declaration expression

Static using

await in a catch block

Exception filter

Conditional access operator for checking null values

Auto property initialzier

The way before

The only way to initialize an auto property,Is to implement an explicit constructor,Set the property value in it.

public class autopropertybeforecsharp6
{
  private string _posttitle=string.empty;
  public autopropertybeforecsharp6 ()
  {
    //assign initial values
    postid=1;
    postname="post 1";
  }
  public long postid {get;set;}
  public string postname {get;set;}
  public string posttitle
  {
    get {return _posttitle;}
    protected set
    {
      _posttitle=value;
    }
  }
}

The way after this feature

Properties with initial values ​​that are automatically implemented using c#6 can be initialized without writing a constructor. We can simplify the above example with the following code:

public class autopropertyincsharp6
{
  public long postid {get;}=1;
  public string postname {get;}="post 1";
  public string posttitle {get;protected set;}=string.empty;
}

2. The main constructor

We use constructors to initialize the values ​​inside.(Accepts parameter values ​​and assigns them to entity attributes).

The way before

public class primaryconstructorsbeforecsharp6
{
  public primaryconstructorsbeforecsharp6 (long postid, string postname, string posttitle)
  {
    postid=postid;
    postname=postname;
    posttitle=posttitle;
  }
  public long postid {get;set;}
  public string postname {get;set;}
  public string posttitle {get;set;}
}

The way after this feature

public class primaryconstructorsincsharp6 (long postid, string postname, string posttitle)
{
  public long postid {get;}=postid;
  public string postname {get;}=postname;
  public string posttitle {get;}=posttitle;
}

In C#6, the main constructor gives us a short syntax for defining constructors with parameters. Each class can have only one main constructor.

If you observe the example above,You will find that we moved the parameter initialization next to the class name.

You may get the following error "feature" primary constructor "is only available in" experimental "language version." (To resolve this problem,We need to edit the solutionname.csproj file to avoid this error. All you have to do is add extra settings after the warningtag

<langversion>experimental</langversion>

"Master Constructor" is only available in "experimental" language versions

3. Dictionary initializer

The way before

The old way of writing a dictionary initializer is as follows

public class dictionaryinitializerbeforecsharp6
{
  public dictionary<string, string>_users=new dictionary<string, string>()
  {
    {"users", "venkat baggu blog"},    {"features", "whats new in c#6"}
  };
}

The way after this feature

We can define a dictionary initializer like the way we use square brackets in an array

public class dictionaryinitializerincsharp6
{
  public dictionary<string, string>_users {get;}=new dictionary<string, string>()
  {
    ["users"]="venkat baggu blog",    ["features"]="whats new in c#6"
  };
}

4. Declaration expression

The way before

public class declarationexpressionsbeforecshapr6 ()
{
  public static int checkuserexist (string userid)
  {
    //example 1
    int id;
    if (! int.tryparse (userid, out id))
    {
      return id;
    }
    return id;
  }
  public static string getuserrole (long userid)
  {
    ////example 2
    var user=_userrepository.users.findbyid (x =>x.userid == userid);
    if (user!=null)
    {
      //work with address ...
      return user.city;
    }
  }
}

The way after this feature

In c#6 you can declare a local variable in the middle of an expression. Using declaration expressions we can also declare variables in if expressions and various loop expressions

public class declarationexpressionsincshapr6 ()
{
  public static int checkuserexist (string userid)
  {
    if (! int.tryparse (userid, out var id))
    {
      return id;
    }
    return 0;
  }
  public static string getuserrole (long userid)
  {
    ////example 2
    if ((var user=_userrepository.users.findbyid (x =>x.userid == userid)!=null)
    {
      //work with address ...
      return user.city;
    }
  }
}

5. Static using

The way before

For your static members,There is no need to make an object instance in order to call a method. You would use the following syntax

typename.methodname
public class staticusingbeforecsharp6
{
  public void testmethod ()
  {
    console.writeline ("static using before c#6");
  }
}

After the way

In c#6, you can use static members without the class name. You can introduce static classes in the namespace.

If you look at the following example,You will see that we moved the static console class into the namespace

using system.console;
namespace newfeatureincsharp6
{
  public class staticusingincsharp6
  {
    public void testmethod ()
    {
      writeline ("static using before c#6");
    }
  }
}

6. await inside the catch block

Before c#6, the await keyword could not be used in catch and finally blocks. In c#6, we can finally use await in these two places.

try
{
 //do something
}
catch (exception)
{
 await logger.error ("exception logging")
}

7. Exception filter

Exception filters allow you to make an if condition before the catch block executes.

Take a look at this example where an exception occurred,Now we want to determine if the exception is null before executing the catch block.

//Example 1
try
{
  //some code
}
catch (exception ex) if (ex.innerexception == null)
{
  //do work
}
//before c#6 we write the above code as follows
//Example 1
try
{
  //some code
}
catch (exception ex)
{
  if (ex.innerexception!=null)
  {
    //do work;
  }
}

8. Conditional access operator?for checking null values.

Take a look at this example,We extract a userranking based on whether the userid is not null.

The way before

var userrank="no rank";
if (userid!=null)
{
  userrank=rank;
}
//or
var userrank=userid!=null?rank:"no rank"

With this feature the way

var userrank=userid?.rank?"no rank";

That's all for this article.Hope everyone likes it.

c
  • Previous __proto__ and prototype in javascript
  • Next C ++ Vector usage in-depth analysis