Menu

Sunday 30 November 2014

Conditional Access in C# 6.0

This feature is very helpful to use in a scenario like to get the property value from the object only if object is not null.
For Example, you are considering delegate Action as the parameter to any of the method, You want to get the name from the Action Method only if Action is not null and Action.Method is not null.
You can easily get an idea by just looking the below code.

public void LogAction(Action action)  
{  
    //Before C# 6.0  
    var actionName = "Empty";  
    if (action != null && action.Method != null)  
    {  
        actionName = action.Method.Name;  
    }  
  
    //C# 6.0  
    var actionName = action?.Method?.Name ?? "Empty";  
  
}  


As per the above code in C# 6.0, action is not null and Method is not null then only "Name" property value is assigned otherwise "Empty" value will be assigned.

5 comments:

  1. Hi,



    I have read your angularjs blog.It"s very attractive and impressive.Very useful to me, I like it your blog
    Thanks for sharing this blog.......

    Thank you
    priya

    ReplyDelete
  2. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.We are providing AngularJs training in velachery.
    For more details: AngularJs training in velachery

    ReplyDelete
  3. Expected to form you a next to no word to thank you once more with respect to the decent recommendations you've contributed here. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition. We are providing AngularJs training in velachry.
    For more details: AngularJs training in velachery

    ReplyDelete