开发者

what does double question marks mean in c# [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

开发者_开发技巧 What is the “??” operator for?

Debugging some code and found ?? inside the code. What does this mean?


?? is the null-coalescing operator for nullable types.

object obj = canBeNull ?? alternative;

// equivalent to:
object obj = canBeNull != null ? canBeNull : alternative;


http://msdn.microsoft.com/en-us/library/ms173224.aspx refer to this for description. it's an operator

The ?? operator defines the default value to be returned when a nullable type is assigned to a non-nullable type.

    // ?? operator example.
    int x = null;

    // y = x, unless x is null, in which case y = -1.
    int y = x ?? -1;

    // Assign i to return value of method, unless
    // return value is null, in which case assign
    // default value of int to i.
    int i = GetNullableInt() ?? default(int);

    string s = GetStringValue();
    // ?? also works with reference types. 
    // Display contents of s, unless s is null, 
    // in which case display "Unspecified".
    Console.WriteLine(s ?? "Unspecified");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜