In this blog, i will be covering 5 useful features of c#, which gives developer more power while writing good and clean code.
The features which we will be discussing in this blog
1. Null Conditional Operator
2. Auto Property Initializer
3. Usage of Static
4. Dictionary Initializer
5. Exception Handling
Let's take a look at each one of above features
1. Null Conditional Operator
It checks the null value for parameter pass as well as checked the collection whether it is null or not. Lets take below example
string name="Dhiraj";
string firstChar=name.CharAtIndex[0]
-- This will return the character at index 0 i.e 'D'.
but if we write the below code
string name;
string firstChar=name.CharAtIndex[0]
- This will throw NullException because there is no value assigned to variable name.
How this can be handled in C# 6.0
string name="Dhiraj";
string firstChar=name?.CharAtIndex[0]:'D';
Here we dont have to check for null value of name field. By default by using ?. by default it will check for nullability of name and then called the function CharAtIndex[0]
and return appropriate value.
2.Auto Property Initializer
This feature helps us in initializing the property mostly read-only property. Read-only property means property having only getter block.
How it is handled before C# 6.0 -
private string name ="Dhiraj";
public string Name {get {return name;}}
How it is handled in C#6.0 -
public string Name {get;}="Dhiraj";
3. Static Usage
Before c#6.0, if we want to use any static method of class, then we have to use as belows
ClassName.MethodName().
Let's take an example of this
namespace MySample
{
public class Mathematics
{
public void static Print(string param)
{
console.writeline("Passed Parameter" + param);
}
}
}
public class MathematicsUse
{
public void MyPrintMethod(string param)
{
Mathematics.Print(param);
}
}
Now Let's try to implement this using C# 6.0 -
using MySample.Mathematics;
public class MathematicsCSharp
{
public void MyPrintMethod(string param)
{
Print(param);
}
}
4. Dictionary Initializer
Before C# 6.0, Dictionary will be initialized and assigned value using below code
var employees=new Dictionary<string,string>()
{
{"Pradeep" ,"Software Developer"},
{"Sandeep" ,"Oracle Developer"},
{"Manjit" , "Doctor"}
};
This is a dictionary which contains list of employees with their profession. Here developer has to provide the key value in bracket .
Now using C# 6.0 , Dictionary will be initialized and assigned value using below code
var employees=new Dictionary<string,string>()
{
["Pradeep"]="Software Developer",
["Sandeep"]="Oracle Developer",
["Manjit"]="Doctor"
};
This is more clean code for assigning values in dictionary.
5. Exception Handling
Before C# 6.0, if developer knows that particular code will throw exception 404 : Page Not found and want to throw some useful exception message , then below code must be written
try
{
throw new Exception("404");
}
catch(Exception ex)
{
if (ex.Message.Equals("500"))
System.Console.Write("Some Ex Messages");
else if (ex.Message.Equals("401"))
System.Console.Write("Some Exceptions Messages");
else if (ex.Message.Equals("404"))
System.Console.Write("Some Exception Messages");
else
System.Console.Write("Some Exception Messages");
}
Now using C# 6.0 - This can be achieved using below code
try
{
throw new Exception("404");
}
catch(Exception ex) when (ex.Message.Equals("500"))
{
System.Console.Write("Some Ex Messages");
}
catch(Exception ex) when (ex.Message.Equals("401"))
{
System.Console.Write("Some Exceptions Messages");
}
catch(Exception ex) when (ex.Message.Equals("404"))
{
System.Console.Write("Some Exception Messages");
}
That's all for this Blog developers ! I hope you found the article useful.
I create content about Programming and Productivity, If this is something that interests you, please share the article with your friends and connections.
Thank you for reading, If you have reached so far, please like the article, It will encourage me to write more such articles. Do share your valuable suggestions, I appreciate your honest feedback!
I would strongly recommend you to Check out my YouTube Channel ( youtube.com/c/codeasitis ) where i put programming video and don't forget to subscribe to my Channel. I would love to connect with you at Twitter ( twitter.com/codeasitis1 ) | Instagram ( instagram.com/codeasitis )
Thank You,