简体   繁体   English

String为Nullable返回false

[英]String is Nullable returns false

Why does: 为什么:

 string s = "";
 bool sCanBeNull = (s is Nullable);
 s = null;

sCanBeNull equate to false? sCanBeNull等于假吗?

I'm writing a code generator and need to ensure every type passed to it is nullable, if it isn't already. 我正在编写代码生成器,并且需要确保传递给它的每个类型都可以为空(如果还没有)。

       //Get the underlying type:
       var type = field.FieldValueType;

        //Now make sure type is nullable:
        if (type.IsValueType) 
        {
            var nullableType = typeof (Nullable<>).MakeGenericType(type);
            return nullableType.FullName;
        }
        else
        {
            return type.FullName;
        }

Do I need to have to explicitly check for a string or am I missing something? 我是否需要明确检查字符串或我错过了什么?

is tells you whether a value is of a particular type, or one derived from that particular type. is告诉您的值是否是特定类型的,或者一个来自该特定类型派生的。

Nullable is a generic struct that allows for nullable versions of non-nullable values. Nullable是一个通用struct ,允许可空值的非可空值。

string is not a Nullable string不是Nullable

To tell if a type can have null values use the fact that for all such types the default value is null , while for all other types it is not: 要判断类型是否可以具有null值,请使用以下事实:对于所有此类类型,默认值为null ,而对于所有其他类型,它不是:

default(string) == null; // true

string is a reference type so it is not Nullable as that is reserved for value types. string是一个引用类型,因此它不是Nullable因为它是为值类型保留的。

In fact: 事实上:

var nullable = new Nullable<string>();

Gives a compile time error. 给出编译时错误。

The type 'string' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable' 类型'string'必须是非可空值类型才能在泛型类型或方法'System.Nullable'中将其用作参数'T'

The rational behind System.Nullable is to be able to represent undefined Value Types using the null keyword. System.Nullable背后的理性是能够使用null关键字表示未定义的值类型 It does not mean that you can check whether some variable can be set to null using someVar is Nullable . 这并不意味着您可以使用someVar is Nullable来检查某个变量是否可以设置为null

If at compile-time you cannot know beforehand whether some variable would be a value-type or a reference-type, you can use: 如果在编译时你无法事先知道某个变量是值类型还是引用类型,你可以使用:

!someVar.GetType().IsValueType

But usually a generic argument would be a better approach. 但通常一般的论点是更好的方法。

Nullable is just a normal class like MyClass or System.String ( Nullable<T> is struct though). Nullable只是一个普通的classMyClassSystem.String (虽然Nullable<T>是struct)。

So if you type: 所以,如果你输入:

class _Nullable {}

struct _Nullable<T> {}

class Program
{
    static void Main()
    {
       string a = "";
       Console.Write(a is _Nullable);
    }
}

You wouldn't be surprised if it returns false right ? 如果它返回false你不会感到惊讶吗?

If you want to check if something is nullable or not you can use if(!(a is ValueType)) 如果你想检查某些东西是否可以为空,你可以使用if(!(a is ValueType))

       string a = "";
       Console.Write("a is nullable = {0}", !(a is ValueType));

Output : a is nullable = true 输出: a is nullable = true

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

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