What are the new features of C# 6.? [Resolved]

Posted by Crniranjanraj under C# on 10/3/2015 | Points: 10 | Views : 626 | Status : [Member] | Replies : 3
What are the new features of C#6.0 ; I am curious to know in simple way with simple example.




Responses

Posted by: Allemahesh on: 10/5/2015 [Member] [MVP] Silver | Points: 50

Up
1
Down

Resolved
New Features in C# 6.0

1. Auto Property Initializer
2. Primary Consturctors
3. Dictionary Initializer
4. Declaration Expressions
5. Static Using
6. await inside catch block
7. Exception Filters
8. Conditional Access Operator to check NULL Values

Auto Property Initializer
1. Auto implemented property with initial value can be initialized without having to write the constructor.
2. Example:-
public long PostID { get;  } = 1;


Primary Consturctors
1. Primary constructor gives us a shortcut syntax for defining constructor with parameters.
2. Only one primary constructor per class is allowed.
3. Example:
public class PrimaryConstructorsInCSharp6(long postId, string postName, string postTitle)

{
public long PostID { get; } = postId;
public string PostName { get; } = postName;
public string PostTitle { get; } = postTitle;
}


Dictionary Initializer

Old Way
public Dictionary<string, string> _users = new Dictionary<string, string>()

{
{"users", "Venkat Baggu Blog" },
{"Features", "Whats new in C# 6" }
};


Using C# 6.0

 public Dictionary<string, string> _users { get; } = new Dictionary<string, string>()

{
["users"] = "Venkat Baggu Blog",
["Features"] = "Whats new in C# 6"
};



Declaration Expressions
1. You can declare an local variable in the middle of the expression.
2. With declaration expressions, we can also declare variables inside if statements and various loop statements.

Old Way

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;
}
}


Using C# 6.0

 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;
}
}



Using Statics
1. You have the ability to use the Static Members without using the type name. You can import the static classes in the namespaces.
2. If you look at the below example, we moved the Static Console class to the namespace:

Old Way

using System;


public void TestMethod()
{
Console.WriteLine("Static Using Before C# 6");
}


Using C# 6.0 Way

using System.Console;

public void TestMethod()
{
WriteLine("Static Using Before C# 6");
}


await Inside catch Block
1. Before C# 6, await keyword is not available inside the catch and finally blocks.
2. In C# 6, we can finally use the await keyword inside catch and finally blocks.
3. Example:-
try 

{
//Do something
}
catch (Exception)
{
await Logger.Error("exception logging")
}


Exception Filters
1. Exception filters allow you a feature to check an if condition before the catch block executes.

Old way:

try

{
//Some code
}
catch (Exception ex)
{
if(ex.InnerException != null)
{
//Do work;
}
}


Using C# 6.0

try

{
//Some code
}
catch (Exception ex) if (ex.InnerException == null)
{
//Do work

}


Conditional Access Operator to Check NULL Values
1. Consider an example that we want to retrieve a UserRanking based on the UserID only if UserID is not null.

Old way

var userRank = "No Rank";

if(UserID != null)
{
userRank = Rank;
}

//or

var userRank = UserID != null ? Rank : "No Rank"



Using C# 6.0

var userRank = UserID?.Rank ?? "No Rank";



Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajnilari2015 on: 10/4/2015 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 25

Up
1
Down
Hi,
A little of google search will help you e.g.

a) http://www.c-sharpcorner.com/UploadFile/8ef97c/full-C-Sharp-6-0-in-single-article-on-visual-studio-2015-preview/

b) http://www.c-sharpcorner.com/UploadFile/8ef97c/list-of-all-new-features-in-C-Sharp-6-0-part-2/

c) http://www.developer.com/net/csharp/slideshows/top-10-csharp-6.0-language-features.html

d) http://blogs.msdn.com/b/csharpfaq/archive/2014/11/20/new-features-in-c-6.aspx

e) https://msdn.microsoft.com/en-us/magazine/dn802602.aspx

are among the few available in the internet.

Refer the first two links which explains well with relevant screen shots. Hope this will be helpful.

--
Thanks & Regards,
RNA Team

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Crniranjanraj on: 10/4/2015 [Member] Starter | Points: 25

Up
0
Down
OK, I ll look into it.Thanks

Crniranjanraj, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response