These features were included as a part of C# in its 6th installment – C# 6.0. Relatively easy to master, they bring a new way of achieving the same programming goals. It can be a little hard to keep up with changing language syntax. But then, that’s the field we chose !
The features I will discuss, are not drastic language changes, but are still a welcome change. These are minor syntactical changes that make coding a little more simpler. Let’s get going.
1. Automatic Property Initialization :
C# had already got rid of the need to declare your private variables before exposing them in properties, which was a few versions ago. In C# 6.0, we go one step ahead see the introduction of auto property initialization that can be done at the time of initialization.
public class MyClass { public string Id { get; } = "assignment"; }
2. Dictionary Initializers
If we look at the way key/value pair is initialized, we see that it is more intuitive and make more sense writing it this way.
Dictionary<string, MyClass> _value = new Dictionary<string, MyClass>() { ["key1"] = new MyClass("value1"); ["key2"] = new MyClass("value2"); }
3. Declaration expressions :
You can declare the local expressions in the body of the loop. There isn’t a need to declare the expressions before the loop begins.
See below snippet where we have the variable – “onlyOnes” declared in the foreach declaration.
int result = 0; foreach(var n in var onlyOnes = numbers.Where(n => n == 1).ToList()) { //do something and populate the result variable. } return result;
4. Use the ‘await’ keyword inside the catch block :
With the async and await keywords, we can pause the execution of the program till the code prefixed with the await keyword is not executed. However, the UI thread is not blocked, and this creates a more responsive UI.
In C# 6.0, the await keyword can now be used inside catch blocks like so,
catch (Exception ex) { await _worker.DoProcess("failed"); }
5. Declaring Static class with other namespaces :
Simply declare the Static class with the other namespaces instead of appending before the method call each time.
using System.Text; using System.Threading.Tasks; using static System.Console; namespace NewNamespace { class Program { static void Main(string[] args) { WriteLine("Hi there !"); } } }
The Console.WriteLine() can be simply invoked as WriteLine() now.
6. Null-Conditional Operator :
Consider the below snippet :
public static string MyFunc(string value1, int value2) { return value1?.Substring(0, 2); }
Assume that the variable – ‘value1’ is null. This will result in NullReference exception when we try to call Substring(). As you can see, we have conditional operator ( ? ) after ‘value1’, which has been introduced in C# 6.0. Basically, this checks whether value1 is null. if it is, then there wouldn’t be any call to Substring() and we can avoid the exception.
Primary constructors were removed from C#6 beta. See https://www.c-sharpcorner.com/UploadFile/7ca517/primary-constructor-is-removed-from-C-Sharp-6-0/.
Yes, I had used the C# 6.0 beta long back as this post is from an older blog of mine that no longer exists.
I have already removed the primary constructor part.
Have you actually used these features? Because at least two of them don’t actually exist!
Primary constructors were considered for C# 6, but eventually dropped. The code you showed doesn’t compile with any current version of C#, not even the preview of C# 8. There is a plan to implement records, which are similar, but for a future version of C#.
Declaration expressions work in some places, but not in a foreach loop. The code you showed doesn’t compile.
Static imports work, but you need to write
using static System.Console
. The code you showed doesn't compile.
Thanks Thomas, I guess I had used the C# 6.0 beta long back as this post is from an older blog of mine that no longer exists.
Appreciate your input. I will change the static import part.