Top best C# language features

C# is an easy, stylish and type-cast object oriented programming language that helps developers to develop a list of safe and robust applications that can execute on .NET Framework.  C# is used for creating windows or client application, database application, client server application and in web services.  The Syntax of C# language is very easy and simple to learn because its code is similar to C and C++ languages. C# syntax remove the complexity of the C++ language and  supply us the good features of it, Moreover it  comfort with generic methods and types.

visualc_banner

Today, I am going to list the best 5 features of the C# 6.0 Language that are expectedly helpful to you. Its available with Visual Studio 2015 Preview for the developer to code them. In Visual Studio VNext we use CTP version 4 , to create the samples of the code.

#Methods and Expressions

As we have seen in code, we have written the method many times for one line code. Now make it in smart way, to create an expression bodied member with only expression instead of using curly braces or use of return.

class Salesman
{
// Method with only the expression
public static int
CalculateMonthlyPay(int dailyWage)
=> dailyWage * 30;
}
  

#? Conditional Access Operator

In earlier C# language version, Before using an object and its property, to check the null value we write down if condition, to make it easier way here is the code.

private void GetMiddleName(Employee employee)
{
string employeeMiddleName = “N/A”;

if (employee != null && employee.EmployeeProfile
!= null)
employeeMiddleName

    employee.EmployeeProfile.MiddleName;
}

#Auto-Property Initializes

Using with this feature, the developer can easily initialize properties without any need of private set and local variable. Here is the sample code of Auto-Property Initializes.

class PeopleManager
{
public List<string> Roles { get; } =
new List<string>() { “Employee”, “Managerial”};
}

#Await in the Catch Block

In C#  the available non-syntactic enhancement that is very important. Await keyword used inside the catch and finally blocks. It handles the async exception handling and at time to call the async process.

public async void Process()
{
try
{
Processor processor = new Processor();
await processor.ProccessAsync();
}
catch (Exception exception)
{
ExceptionLogger logger = new ExceptionLogger();
// Catch operation also can be aync now!!
await logger.HandleExceptionAsync(exception);
}
}

#Exception Filters

Exceptions can be remove in the catch block in easier way with C# 6. Here is sample codewhere we can handle all Exception expect of one SqlException type.

public async void Process()
{
try
{
DataProcessor processor = ne
}
// Catches and handles only non sql exceptions
catch (Exception exception) if(exception.GetType()
!= typeof(SqlException))
{
ExceptionLogger logger = new ExceptionLogger();
logger.HandleException(exception);
}
}

 

 

 

 

 

Spread the love...Share on FacebookShare on Google+Pin on PinterestShare on RedditTweet about this on TwitterEmail this to someone