How to use Coalescing operator in C#

No.of Views2227
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  16 Jan 2011 01:01:19
Tag : CSharp , CSharp4.0
In this article I will explain how to use Coalescing operator in C#. The Coalescing operator is short cut instead of IF condition.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article I will explain how to use Coalescing operator in C#. The Coalescing operator is short cut instead of IF condition.Coalescing operator is new operator added in C#2.0. Coalescing operator is also known as ??.

How Coalescing operator works?

Note

Here I am just going to show how coalescing operator replace ternary operator. i.e not comparing ternary operator with coalescing operator.

Ternary Operator

Syntax

Type variable = booleanCondition ? exp1 : exp2;

Ternary operator assign exp1 to variable if the booleanCondition return true or exp2 if booleanCondition return false.Consider Case were I am coding with the nullable type using ternary opertor.

Example

Nullable<int> a = null;
Nullable<int> b = 10;
int c = a==null ? b.Value : a;

Coalescing operator

Coalescing operator work some what similar to ternary operator but it works only with the Nullable types only. so its sort hand operator to deal with Nullable types only.

Syntax

Type variable = nullalbeTypeVariable1.Value ?? nullableTypeVariable2.Value;

Operator its check value of nullalbeTypeVariable1 if its as real value rater tan null it assin value to variable else assign value of nullableTypeVariable2.

Example

Nullable<int> a = null;
Nullable<int> b = 10;
int c = a.Value ? b.Value : a;

Summary

Coalescing operator deals with Nullable type and its sort for long ternary operator expression.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Expert
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    In this article, i will explain how to use the How to use ExpandoObject Class in .NET 4.0.The ExpandoObject class is introduced on .NET Framework 4.0 and inherited using many interfaces.
    Published Date : 16/Apr/2011
    C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects
    Published Date : 26/May/2010
    Dynamic Datatype in C# 4.0 is quit bit familiar to Var Datatype. Deference between Var and Dynamic is VAR initialized on CompileTime and Dynamic initialized On Runtime
    Published Date : 17/May/2010
    In this article I will show how to use the IObserver and IObservable Interfaces in C#. These two interfaces is work with connection to Push based approach on Reactive Framework
    Published Date : 15/Dec/2010
    Through this article, i will introduce in FCL named Tuple which can store n - number of values in it. Yes, you specify the type of each of those variables as generic parameters, and the object will create those values for you
    Published Date : 15/Nov/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top