简体   繁体   English

c#2.0中可为空值的默认值

[英]Default value for nullable value in c# 2.0

Using C# 2.0, I can specify a default parameter value like so: 使用C#2.0,我可以指定一个默认参数值,如下所示:

static void Test([DefaultParameterValueAttribute(null)] String x) {}

Since this C# 4.0 syntax isn't available: 由于此C#4.0语法不可用:

static void Test(String x = null) {}

So, is there a C# 2.0 equivalent for value types? 那么,值类型的C#2.0是否相同? For instance: 例如:

static void Test(int? x = null) {}

The following attempts don't compile. 以下尝试无法编译。

// error CS1908: The type of the argument to the DefaultValue attribute must match the parameter type
static void Test([DefaultParameterValueAttribute(null)] int? x) {}

// error CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression
static void Test([DefaultParameterValueAttribute(new Nullable<int>())] int? x) {}

Unfortunately, the older versions of the C# compiler do not support this. 不幸的是,旧版本的C#编译器不支持这一点。

The C# 4.0 compiler compiles this: C#4.0编译器编译如下:

public static void Foo(int? value = null)

Into: 成:

public static void Foo([Optional, DefaultParameterValue(null)] int? value)

This is effectively the same as your first attempt (with OptionalAttribute added, in addition), which the C# 2 compiler errors on with CS1908, as this wasn't supported directly in that version of the compiler. 这实际上与您第一次尝试(另外添加了OptionalAttribute )相同,C#2编译器在CS1908上出错,因为在该版本的编译器中不直接支持。

If you need to support C# 2, in this case, I would recommend adding an overloaded method instead: 如果您需要支持C#2,在这种情况下,我建议您添加重载方法:

static void Test()
{
    Test(null);
}
static void Test(int? x)
{
    // ..

Reed is of course correct; 里德当然是正确的; I just thought I'd add an interesting fact about a corner case. 我只是想我会在一个角落案例中添加一个有趣的事实。 In C# 4.0 you can say: (for struct type S) 在C#4.0中,您可以说:(对于结构类型S)

void M1(S x = default(S)) {}
void M2(S? x = null) {}
void M3(S? x = default(S?)) {}

But oddly enough you cannot say 但奇怪的是你不能说

void M4(S? x = default(S)) {}

In the first three cases we can simple emit metadata that says "the optional value is the default value of the formal parameter type". 在前三种情况下,我们可以简单地发出元数据,其中“可选值是形式参数类型的默认值”。 But in the fourth case, the optional value is the default value of a different type . 但在第四种情况下,可选值是不同类型的默认值。 There is not an obvious way to encode that kind of fact into metadata. 没有一种明显的方法可以将这种事实编码到元数据中。 Rather than coming up with a consistent rule across languages for how to encode such a fact, we simply made it illegal in C#. 我们只是在C#中将其设置为非法,而不是针对如何编码这样的事实提出跨语言的一致规则。 It's likely a rare corner case, so no great loss. 这可能是一个罕见的角落案件,所以没有很大的损失。

Apparently you can't use this attribute. 显然你不能使用这个属性。

As you know attribute parameters are 'serialized' to metadata at compile time - so you need constant expression. 如您所知,属性参数在编译时被“序列化”为元数据 - 因此您需要持续表达式。 Since the compiler doesn't like 'null' you're out of options. 由于编译器不喜欢'null',因此你没有选择。

What you can do is - overload - define another Text method with no parameters which calls your Text method with null 您可以做的是 - 重载 - 定义另一个没有参数的Text方法,该方法使用null调用Text方法

Does this work: [DefaultParameterValueAttribute((int?)null)] ? 这是否有效: [DefaultParameterValueAttribute((int?)null)] Tried - this doesn't work too ( 试过 - 这也行不通(

我相信它应该是:

static void Text(int? x = default(int?));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM