An Unknown feature in C# Maybe Just For Me

Possible Duplicate:
Hidden Features of C#?

What is it? Is it useful? Where?

??

It work this way;

Object a = otherObject ?? "otherObject is null";

It means otherObject will be assigned to "a" if it is not null. If it is null the object at right will be assigned to "a".

Its useful for me when I wanna set default values;

public class MyClass
{
    private Object b;
    public MyClass(Object a)
    {
        b = a ?? "Deafult Value";
    }
}

Official documentation can also be found here; MSDN


This is the null-coalescing operator and allows you to set a default value if the object is null.

http://msdn.microsoft.com/en-us/library/ms173224.aspx


object o = someObject ?? anotherObject;

是一样的

object o;
if(someObject == null)
  o = anotherObject;
else
  o = someObject;
链接地址: http://www.djcxy.com/p/52948.html

上一篇: C#或.NET中见过的最奇怪的角落案例是什么?

下一篇: C#中的一个未知功能可能只是为了我