简体   繁体   English

为什么类型为null的值不能用作double类型的默认参数?

[英]Why a value of type null cannot be used as a default parameter with type double?

Quick Question: 快速问题:

MSDN - Named and Optional Arguments (C# Programming Guide) states clearly that MSDN - 命名和可选参数(C#编程指南)明确指出

" Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors , and delegates." 可选参数使您可以省略某些参数的参数。这两种技术都可以与方法,索引器, 构造函数和委托一起使用。”

So instead of this: 所以不是这样的:

class MyClass
{

//..

public MyClass() { // Empty Constructor's Task }
public MyClass(SomeType Param1) { // 2nd Constructor's Task }
public MyClass(SomeType Param1, SomeType Param2) { // 3rd Constructor's Task }
}

I should be able to do this: 我应该能够做到这一点:

class MyClass
    {
        //..

        public MyClass(SomeType Param1 = null, SomeType Param2 = null)
        {
            if (Param1)
            {
                if (Param2)
                {
                    // 3rd constructor's Task
                }
                else
                {
                    // 2nd constructor's Task
                }
            }
            else
            {
                if (!Param2)
                {
                    // Empty constructor's Task
                }
            }

        }
    }

Then why this is not working: 那为什么这不起作用:

public MyClass(double _x = null, double _y = null, double _z = null, Color _color = null)
{
   // ..
}

Telling me: 告诉我:

A value of type "null" cannot be used as a default parameter because there are no standard conversions to type 'double' “null”类型的值不能用作默认参数,因为没有标准转换来键入“double”

double is a value type . double值类型 You'd need to wrap it in Nullable<T> or ? 你需要将它包装在Nullable<T>? for shorthand, to indicate that it is nullable. 简而言之,表示它可以为空。

public MyClass(double? _x = null, double? _y = null, double? _z = null, Color _color = null)
{
   // ..
}

As David explained in his answer, Double is not a nullable type. 正如大卫在他的回答中解释的那样,Double不是一个可以为空的类型。 In order to assign it a null value, you'll have to convert Double to System.Nullable<double> or double? 为了赋予它一个空值,你必须将Double转换为System.Nullable<double>double?

The full answer will look something like: 完整的答案将如下所示:

public void MyMethod(double? param = null) { }

The obvious problem here is that instead of just passing it a double value, you'll instead have to pass it a double? 这里显而易见的问题是,不是只传递一个double值,而是必须传递一个double? value instead. 价值而不是。

I'm not sure of the exact scope of this functionality but you can always refer to defaults instead. 我不确定此功能的确切范围,但您总是可以参考默认值。 For instance: 例如:

public void MyMethod(double param = double.MinValue) 
{ 
    if (param == double.MinValue) 
        return; 
}

Or something like that. 或类似的东西。

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

相关问题 值类型字符串不能用作默认参数 - Value type string cannot be used as default parameter 类型为“<null>”的值不能用作默认参数,因为没有标准转换来键入“T” - A value of type '<null>' cannot be used as a default parameter because there are no standard conversions to type 'T' '?' 类型的值不能用作默认参数 | .net 6 与 .net 4.8 - A value of type '?' cannot be used as a default parameter | .net 6 vs .net 4.8 类型为'int'的值不能用作默认参数,因为没有标准转换来键入C# - A value of type 'int' cannot be used as a default parameter because there are no standards conversions to type C# 值不能为空。 参数名称:类型 - Value cannot be null. Parameter name: type 类型参数不能与类型参数一起使用 - The type parameter cannot be used with type arguments 通用方法-类型不能用作类型参数 - Generic method - Type cannot be used as Type Parameter 为什么不能在c#中将null转换为类型参数T? - Why cannot convert null to type parameter T in c#? 在LINQ dat source / gridview中使用文本框值作为参数:“String”类型的值无法转换为“Double”类型 - Using textbox value as parameter in LINQ dat source/gridview : A value of type 'String' cannot be converted to type 'Double' 值不能为空。 参数名称:键入DotNetNuke - Value cannot be null. Parameter name: type in DotNetNuke
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM