What’s New in C# 5.0?

This month’s is about the top 6 new features of C# 6.0. What are the best features? How has the language changed? Welcome to the latest news from TalkIT.

Contents

What’s New in C#? 6 Top New Features Can You Solve This Coding Problem? Other Bits New Course Outlines  

What’s New in C#?

We introduced the new features of C# 5.0 in the January 2015 Newsletter. These are just my ideas, based on 12 years experience training and developing. As always your ideas are appreciated. Just add your comments at the bottom of the blog. It is interesting how the language has grown over the last decade. The initial vision for the language was to keep the number of C# keywords small, but with each new addition keywords have been added. Its creator Anders Hejlsberg intended it to be a simple, modern, general-purpose, object-oriented programming language. New features are added, with each version of the .Net framework. These allow C# to keep pace with other popular languages like C++ and Java. They also aim to make the syntax even more concise. This is particularly true for the OOP syntax. With each new version this gets more condensed. Many of these changes are based on feedback from developers. I did not find any big changes with 6.0. Most of the changes were about condensing or clarifying syntax. Nothing major like Lambda expressions (which came with C# 3.0) is added. C#Features Here is a summary of new features (the top 6 at the top).

  1. Auto-Properties with Initializers
  2. Static Using Statements
  3. String interpolation
  4. Nameof Expressions
  5. Indexed Initializers
  6. Null-conditional operators
  7. Expression bodies on method-like members
  8. Exception filters
  9. Await in catch and finally blocks
  10. Improved overload resolution

C# 6.0 comes with the .Net Framework 4.6 in Visual Studio 2015. When I tried using C# 6 with Visual Studio 2012 and 2013 I hit problems. I attempted to use Visual Studio’s multi targeting option to point to .Net 4.6. But Visual Studio refused to cooperate with this. So I resorted to a (long!) download of the Visual Studio 2015 Community edition. Here is an evolution matrix. Find out more from the TalkIT course in C#: https://www.talk-it.biz/c-marketing-page/

6 Top New Features

1. Initializers for auto-properties

You can now initialize a property, just as you can in a field:index

public class Customer
{
public string First { get; set; } = "Jane";
public string Last { get; set; } = "Doe";
}

 The property will be read only when the setter is left out.

public class Customer
{
public string First { get; } = "Jane";
public string Last { get; } = "Doe";
}

Read only properties can be assigned in the type’s constructor,

public class Customer
{
public string Name { get; };
public Customer(string first, string last)
{
Name = first + " " + last;
}
}
2. Using static

The feature allows the static members of a type to be imported, making them available without qualification in code:

using static System.Console;
using static System.Math;
using static System.DayOfWeek;
class Program
{
static void Main()
{
WriteLine(Sqrt(3*3 + 4*4));
WriteLine(Friday - Monday);
}
}

This is helpful when you have a set of functions in a class that you frequently use. System.Math is a good example of this. This also lets you access named values of an enum type, like the System.DayOfWeek members above.

3.  String interpolation

String.Format is useful but clumsy. Those {0} etc. placeholders in the format string must line up with the other arguments.

var s = String.Format("{0} is {1} year{{s}} old", p.Name, p.Age);strings

String interpolation puts the expressions in their right place.

var s = $"{p.Name} is {p.Age} year{{s}} old";

Alignment and format specifiers can be supplied.

var s = $"{p.Name,20} is {p.Age:D3} year{{s}} old";

The expressions can even contain other strings.

var s = $"{p.Name} is {p.Age} year{(p.Age == 1 ? "" : "s")} old";
4. nameof expressions

Occasionally you need the name of a variable or property. When throwing an ArgumentNullException you may want to name the guilty argument.

if (x == null) throw new ArgumentNullException(nameof(x));Name Of

You can use more elaborate names, but only the final identifier will be used.

WriteLine(nameof(person.Address.ZipCode)); // prints "ZipCode"

5. Index initializers

Object and collection initializers are concise way to provide initial values. Initializing dictionaries and other objects with indexers was less graceful. Here is the new syntax to set values to keys through an indexer:

var numbers = new Dictionary<int, string> 
{
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
6. Null-conditional operators

The null-conditional operator (?) lets you access members only when they are not null. Else a null result is returned.null

int? length = customers?.Length; // null if customers is null
Customer first = customers?[0]; // null if customers is null

The null-conditional operator is conveniently used with the null coalescing operator (??):

int length = customers?.Length ?? 0; // 0 if customers is null

Null-conditional operators can be chained, if there is a need to check for null more than once:

int? first = customers?[0].Orders?.Count();

Can you solve this coding problem?

 Practice you C# 6.0 skills. What is the output? Assume we have imported the relevant types.Problem

string Name = "Jane Doe"; int Age = 23;
WriteLine(Sqrt(3 * 3 + 4 * 4));
WriteLine(Friday - Monday);
WriteLine($"{Name,4} is {Age:D2} years old");

Can you add code snippets to demonstrate the other features in this blog?

 Other Bits

TalkIT has been very active on social media recently. We have been posting on coding, new gadgets and IT humour. Why don’t you connect with us on Twitter or FaceBook? You can follow all the latest news and let us know what you think.Bits Twitter @NowTalkIT FaceBook NowTalkIT   6 Coding Puzzles Teach You How to Read IF statements. http://talk-it.biz/2015/04/6-codi  Get inspired and learn coding today! #javascript #html5 #csharp Courses http://talk-it.biz  #learncoding Your code is magic! programming courses http://www.talk-it.biz/  #javascript #html5 #java #csharp #python #mvc Time management.Learn how to prioritize and meet #coding deadlines http://ow.ly/Q7tVm  #javascript #talkIT #html5 How do you build enterprise websites using #aspnet #mvc? http://www.talk-it.biz/2014/05/talkit-june-newsletter/ …#programming #csharp #webdesign How are interfaces used in OO design?Find out more on http://ow.ly/Q7BdE  #Csharp #elearning #coding #javascript Creating multiple threads in #Java http://youtu.be/z_6YPyaXGg0  check our complete course on http://talk-it.biz/tutorial-categories/java/ … #elearning #coding #tutorial Simultaneous Execution Using Asynchronous Programming in #Csharp http://ow.ly/IGADq  Funny Developer habits. Do you notice some of these? http://www.talk-it.biz/2015/03/talkit-humour-developer-habits/ …

New Course Outlines

I have updated the TalkIT course outlines. There are some interesting new classroom courses. Plus the existing courses now refer to the latest version of the software. You can find a full list of outlines on the enhanced Talk-IT website. Just click http://talk-it.biz/training/courses/ Do take a look and feel free to let me know what you think. Here are popular course outlines: Training in ASP.NET MVC 6 Development Training in C# 6 Development Training in SQL Server Reporting Services New online courses in Java, C++ and Python have been added to the TalkIT site recently. Take an advanced look at what is in the courses. C++ http://talk-it.biz/course/c-programming-training/ Python http://talk-it.biz/course/python-programming/ In September we will publish an additional Java tutorial. This teaches you Java by building a complete game application.

 Thanks to GitHub for code samples

Photos www.freedigitalphotos.net/

David Ringsell MCPD 2015 ©

 

Scroll to Top
Share via
Copy link