读书人

请教下面这个问号怎么 解释?C#语法

发布时间: 2012-04-10 21:03:56 作者: rapoo

请问下面这个问号如何 解释??C#语法真难以理解
public static void Insert(string varfileName,string vardescription,DateTime? vardateX,int? varalbumSortId)



[解决办法]
可空类型可以包含值,或者可以是未定义的。?? 运算符定义当可空类型分配给非可空类型时返回的默认值。如果在将可空类型分配给非可空类型时不使用 ?? 运算符,将生成编译时错误。如果使用强制转换,并且当前未定义可空类型,将发生 InvalidOperationException 异常。

有关更多信息,请参见可为空的类型(C# 编程指南)。

示例
复制代码
// nullable_type_operator.cs
using System;
class MainClass
{
static int? GetNullableInt()
{
return null;
}

static string GetStringValue()
{
return null;
}

static void Main()
{
// ?? 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 ");
}
}


[解决办法]
加个?号表示可以为空
[解决办法]
加个?号表示可以为空

读书人网 >asp.net

热点推荐