C# 7.0 – #6. Usage expanded for statement “throw”

Until C# 6.0 the “throw” keyword must be used only as a standalone statement, that is it cannot be part of an expression. C# 7.0 overcomes this limitation, and then allows this keyword to be placed everywhere, e.g. in the middle of a ternary operator:

string  a = null;
    // this row doesn't compile in C# 6.0 because of throw statement
    a = a !=  null ? a = " some value" : throw new NullReferenceException("a");
catch (Exception ex)
    a = "some other value";
    Console.WriteLine("New throw feature: " + a);  // prints "some other value"

One thought on “C# 7.0 – #6. Usage expanded for statement “throw”

Leave a Reply

Your email address will not be published. Required fields are marked *