Denis Jakus

making tech less cryptic

CTO x Unique People

CTO Note

C# 6.0 new features2 min read

10 September 2017 .NET

With C# 6.0 we got new features which are not revolutionary but nonetheless are pretty handy improvements that helps us simplify our code.

So let’s list these new features:

  • Using Static
  • Expression Bodied Members
  • Getter-only Auto-Properties
  • Auto-property Initializers
  • Dictionary Initializers
  • Null-conditional Operator
  • String Interpolation

Using Static

We can import static classes with using statemens:

i.e. If we have some Foo class with static method Bar, we can write the following code:

[syntax_prettify]using static Foo[/syntax_prettify]

and then we can write our code like:

[syntax_prettify linenums="linenums"]public class Program {
    public static void Main() {

Expression Bodied Members

With this new feature we use “=>” notation to implement getters right away. We can also implement simple methods aswell.


[syntax_prettify linenums="linenums"]public class OrderedItem  {   
  public int DaysSinceOrder => (OrderCreated - DateTime.Today).TotalDays;
} [/syntax_prettify]

Getter-only Auto-Properties

With this new feature we can reduce code by not using private set when defining read-only auto-property.


[syntax_prettify linenums="linenums"]public class Item  {
    public int Rate { get; }

Auto-property Initializers

This one helps us with initialization without the need for creating a constructor.


[syntax_prettify linenums="linenums"]public class Order
public DateTime OrderCreated { get; private set; } = DateTime.Now;
public Collection OrderItems { get; private set; } = new Collection();

Dictionary Initializers

This feature gives us  index like syntax for dictionaries initialization, hint: no more curly braces.


[syntax_prettify linenums="linenums"]var names = new Dictionary
["DJ"] = "Denis Jakus",
["FL"] = "First Name, Last Name"

Null-conditional Operator

This one is definetly the most usefull and by far my favorite. It’s used to check for NullReferenceException, swift alike syntax which is also very handy when dealing with triggering events.


[syntax_prettify]var firstName = user?.FirstName; // if user is not null return user FirstName [/syntax_prettify]


[syntax_prettify]OnClick?.Invoke(this, args);[/syntax_prettify]

String Interpolation

My second favorite feature is this one used for concatenating strings. No more need to use String.Format.

Just use “$” in front of quotations and user curly braces to use object properties. Much simpler and cleaner!


[syntax_prettify]var fullName = $"{user.FirstName} {user.LastName}";[/syntax_prettify]


As you can see, these are non revolutionary features but can greatly improve readability and make our code much cleaner.

Related Posts
How to organize Visual Studio projects

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like In my latest Youtube video,…

Caching in DotNet Core WebAPI using Strategy Pattern

Facebook Twitter Evernote Hacker News Gmail Print Friendly Yahoo Mail reddit LinkedIn Blogger Tumblr Like Let me write today about…

Write a comment
Exit mobile version