dev csharp

Conclusion and List of Posts

General consensus is that the new features are just small increments to improve productivity. They will help to clean up existing code. Less code is helpful to focus on the actual business logic instead of the clutter caused by the language.

For easy navigation I listed the links for all the previous posts:

Table of contents

  1. C# 6.0 New Features - Introduction
  2. Auto-Properties with Initializers
  3. Using statements for static classes
  4. Expression-bodied methods
  5. String interpolation
  6. Index initializers
  7. Null-conditional operators
  8. nameof operator
  9. Exception-handling improvements

Resources

dev csharp

There are 2 improvements on exception handling:

  1. Exception filters
  2. Using await in catch and finally blocks

Exception Filters

Visual Basic and F# already have this feature and now C# has it too! the way it works is basically defining a condition for the catch block (example taken from Channel 9 video):

try
{

}
catch(ConfigurationException e) if (e.IsSevere)
{

}

I think it can make exception handling more modular. Also it’s better than catching and rethrowing in terms of we don’t lose information about the original exception.

Using await in catch and finally blocks

Like most people I hadn’t noticed we couldn’t do that already! Apparently it was just a flaw in the current implementation and they closed that gap with this version

try
{

}
catch(ConfigurationException e) if (e.IsSevere)
{
	await LogAsync(e);
}
finally
{
	await CloseAsync();
}

dev csharp

Personally I think this one is a bit trivial. So the argument is it eliminates the need for using hard-coded strings in the code.

For instance:

public class NameofOperator
{
    public void Run(SomeClass someClass)
    {
        if (someClass == null)
        {
            throw new ArgumentNullException("someClass");
        }
    }
}

public class SomeClass
{
}

Say you refactored the code and changed the parameter name in this example. It is likely to forget changing the name in the exception throwing line since it has no reference to the actual parameter.

By using nameof operator we can avoid such mistakes:

public class NameofOperator
{
    public void Run(SomeClass refactoredName)
    {
        if (refactoredName == null)
        {
            throw new ArgumentNullException(nameof(refactoredName));
        }
    }
}

public class SomeClass
{
}

The results are identical but this way when we change a parameter name all references to that object will be updated automatically.