Monday, 15 May 2017

Exception Filters in C# 6.0

C# 6.0 introduces a new feature exception filter. As per MSDN “Exception Filters are clauses that determine when a given catch clause should be applied. If the expression used for an exception filter evaluates to true, the catch clause performs its normal processing on an exception.If the expression evaluates to false, then the catch clause is skipped.”
1
2
// Syntax
catch (Exception ex) when (FilterIsTrue).
C# 6.0 exception filter and when keyword is not a syntactic sugar. It is a CLR feature. Unlike catch block, exception filter does not unwind the call stack, which is helpful at runtime.
So what is Stack unwinding?
Stack unwinding
When you enter a catch block, the stack is unwound: this means that the stack frames for the method calls “deeper” than the current method are dropped. This implies that all information about current execution state in those stack frames is lost, making it harder to identify the root cause of the exception.
Exception filter does not unwind the call stack.
See the screen shot.
Blog20_01

Sunday, 14 May 2017

Null-conditional operators in C# 6.0

Null-Conditional operator enable developers to check the null value with in an object reference chain. The null – conditional operator ( ?.) , returns null if anything in the object reference chain in null. This avoid checking null for each and every nested objects if they are all in referenced and having null values.
The expression A?.B evaluates to B if the left operand (A) is non-null; otherwise, it evaluates to null.
1
2
3
4
5
6
public void NullConditionalOPerator()
{
    string name = null;
    var length = name?.Length;
    Console.WriteLine(length);
}

using static in C# 6.0

Often we use a class’s static methods throughout our code. Repeatedly typing the class name can obscure the meaning of your code. A common example is when you write classes that perform many numeric calculations. Your code will be littered with @System.Math.Sin, @System.Math.Sqrt and other calls to different methods in the Math class.
In C# 6.0 , we can go one step further and import the static members of a class into our namespace.This means we can use the static members of a class directly with no need to qualify them with their namespace or type name.
1
2
    using static System.Math;
using static System.Console;
it can eliminate a lot of redundant or superfluous type identifiers in the code and thus help improve readability
1
2
3
4
5
public void UsingStaticFeature()
{
    var power = Pow(2, 2);
    WriteLine(power);
}

Expression-bodied function members in C#

I am exploring new features introduced in C#. In last post, I looked at Auto-Property enhancements in C# . Now I am exploring “Expression-bodied function members” feature.
Lets get our hand dirty with this new feature. Many times we have methods whose body consist on single line of code like WriteLog, Add two number etc.
1
2
3
4
5
6
7
8
9
public int Add (int first,int second)
{
    return first + second;
}
 
public void WriteLog()
{
    Console.WriteLine(Message);
}
Expression-bodied function members allow properties, methods and other function members to have bodies as lambda like expressions instead of statement blocks. Thus reducing lines of codes and clear view of the expressions.It works for methods and read-only properties.
1
2
3
public string Message { get; set; } = "Message";
        public int AddEx(int first, int second) =>; first + second;
        public void WriteLogEx() =>; Console.WriteLine($"{Message}");