What is new in C# 6.0?

What is new in C# 6.0?

In the 6.0 version of the C# programming language, the main focus of language designers were on adding some features to eliminate its redundancy and increase the clarification, conciseness and readability. Alongside adding these features to the 6.0 version, they have been developing Roslyn as an open-source compiler for C# and Visual Basic programming languages to building code analysis tools!


New features of C# 6.0:

No alt text provided for this image


  • Static imports:

By occupying the “using static <fully-qualified-type-name>” signature, we can access the specified to its static members and their nested types as well without mentioning the type name in all invocations. And if we don’t mention the “using static <fully-qualified-type-name>” and try to use its members, then the compiler will raise the CS0246 error!

No alt text provided for this image

We know that the System namespace has many libraries and nested classes in itself. The ability to append all the class of a namespace or their members to the program prevents multiple repetitions. It also makes it easier, clearer and more readable to read and write the programs.

As you can see in this sample code, if I don't add the "using static System.Math" on the top of the programme, I must use its static members by invoking the Math class on all invocations. but by adding this class of the namespace, we can easily access the PI constant value(as a static member of this namespace) without mentioning the Math class in every invocation of this member.

  • Exception filters:

This feature is the use of the “when” contextual keyword in three main situations to apply a filter or set of filters on the input statement:

  1. Catch block of the try/catch or try/catch/finally
  2. Case branches of the switch statement
  3. Switch expressions (which is defined in the C# 8.0) 
  • Auto-property initializer:

properties are one of the members of the class, and they are accessed like fields but they implement the "get" and "set" accessors as well when they want to change their values. in the sample code below, the FirstName is a property of the person class. At the compilation, the compiler will consider the storage location for this field and implement s the body of the "{get; set;}" accessors. So they are called the auto-property syntax for this field.

public class Person
{
    public string FirstName { get; set; }

    // remaining implementation removed from listing
}

  • Expression bodied members:

An expression body definition provides a more clear and readable definition of the expressions:

member => expression;

// the member is the set of parameter
// the expression must be a valid expression



// An example for this feature:

public override string ToString() => $"{fname} {lname}".Trim();


// This expression is the shortened for of this expression:

public override string ToString()
{
   return $"{fname} {lname}".Trim();
}

This form of definition was introduced for Method and Read-only properties in C# 6.0 and for Property, Constructor, Finalizer and Indexers in the C# 7.0 versions.

  • Null propagator

In the C# 6.0 version, there are introduced two operations check the nullability of the members of a class with "?." and members of a collection with "?[]" signs.

  • if a is a null-value member of a class of collection, then a?. and a?[] return the null value
  • if a is a non-null value of the then the a?.x is the same as a.x and a?[x] is the same as a[x]
  • String interpolation

In C# 6.0 and later we can use the "$" special character to construct a string clause consisting of a fixed expression containing some variable items in it. These items could be replaced with the variables which have been defined former to this clause.

There are two patterns to interpolate a string clause that you can see in the snippet below:

string name = "Mark";
var date = DateTime.Now;

// Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);

// String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");

// Both calls produce the same output that is similar to:
// 

Hello, Mark! Today is Wednesday, it's 19:40 now.

Except for this feature of C# 6.0, we can use other alternatives as string composite formatting features like String.Format, StringBuilder.AppendFormat, Cosole.WriteLine, etc

Note: for writing this article I have studied multiple Microsoft documents which are related to the title of this article. The code snippets are adapted from their examples as well.

Mostafa Jafary

Professor associate, Academic Dean ( Department of Management & accounting) at University of Zanjan

3 年

????? ???? ?????. ?????? ????? ??? ?? ? ????? ????? ?? ?? ??????

要查看或添加评论,请登录

Elahe Dorani的更多文章

  • What's new in C# 8.0?

    What's new in C# 8.0?

    Introduction C# as a type-safe, object-oriented and component-oriented programming language is at the heart of the .Net…

  • What's new in C# 7.0 through 7.3?

    What's new in C# 7.0 through 7.3?

    Introduction In C# version 7.0 which is released within Visual Studio 2017, we are facing some newer and cooler…

  • Chapter 18: Asynchronous Language Features

    Chapter 18: Asynchronous Language Features

    Introduction The CPU of digital gadgets are the brain of devices. The operating systems on them would consider an…

    1 条评论
  • Chapter 10: LINQ

    Chapter 10: LINQ

    Introduction LINQ, the Language Integrated Query, is an integrated set of technologies using to apply some queries on…

    3 条评论
  • Chapter 9: Delegates, Lambdas, and Events

    Chapter 9: Delegates, Lambdas, and Events

    Introduction Delegates are some types that provide a reference to the methods. Events are some notification occurred in…

  • Chapter 8: Exceptions

    Chapter 8: Exceptions

    Introduction In the execution process of any programs, there could occur some errors and it is on the CLR decide to run…

  • Garbage Collection

    Garbage Collection

    The garbage collector is an automatic mechanism to detect and collect the dead object’s memory space and make it…

  • Chapter 7: Object Lifetime

    Chapter 7: Object Lifetime

    Introduction C# language programming supports the object-oriented paradigm. It means that almost all operations in this…

  • Chapter 6: Inheritance

    Chapter 6: Inheritance

    C# as an object-oriented language has some essential features which make it possible to produce object-oriented…

  • chapter 5: Collections

    chapter 5: Collections

    Introduction The data is the main concept we want to deal with in every program we write. The most important thing we…

社区洞察

其他会员也浏览了