2014-12-06

New Features in C#

There are two new features showing up in the next version of C#. Actually, there are more than two, but I find these interesting. They are still being thrashed out, but are nearing final form.

The first is the nameof() operator, that will give you the name of the thing in the parentheses. A typical example of using it is:

void f(string s)
{
    if (s == null)
    {
        throw new ArgumentNullException(nameof(s));
    } 
}

The second is string interpolation. This allows you to do

string foo = $"{name} works in {department} and has {employeeCount} employees";

rather than

string foo = string.Format("{0} works in {1} and has {2} employees", name,
    department, employeeCount);

The exact details are in the links.

Also interesting is the discussion about these as they work out the details. You can find the details of the design decisions on the CSharp Language Design Notes page. The process they are going through to design these features gives you insight into the features themselves. It is cool to see this being done in public rather than in some conference room in Redmond.

No comments :

Post a Comment

Note: Only a member of this blog may post a comment.