usage of double question mark in .net
Possible Duplicate:
What do two question marks together mean in C#?
What is the usage of ?? in .Net? How it can be used to check while assigning the variables? Could you please write some code snippet to better explain the content ? Is it related with some nullable ?
The operator '??' is called null-coalescing operator, which is used to define a default value for a nullable value types as well as reference types.
It is useful when we need to assign a nullable variable a non-nullable variable. If we do not use it while assigning, we get an error something like
Cannot implicitly convert type 'int?' to 'int'. An explicit conversion exists (are you missing a cast?)
To overcome the error, we can do as follows...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace GenConsole
{
class Program
{
static void Main(string[] args)
{
CoalescingOp();
Console.ReadKey();
}
static void CoalescingOp()
{
// A nullable int
int? x = null;
// Assign x to y.
// y = x, unless x is null, in which case y = -33(an integer selected by our own choice)
int y = x ?? -33;
Console.WriteLine("When x = null, then y = " + y.ToString());
x = 10;
y = x ?? -33;
Console.WriteLine("When x = 10, then y = " + y.ToString());
}
}
}
它是空合并运算符。
链接地址: http://www.djcxy.com/p/53838.html上一篇: 什么是 ?? 在我的财产?
下一篇: 在.net中使用双重问号