c# – String是Nullable返回false

前端之家收集整理的这篇文章主要介绍了c# – String是Nullable返回false前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么:
string s = "";
 bool sCanBeNull = (s is Nullable);
 s = null;

sCanBeNull等于假吗?

我正在编写代码生成器,并且需要确保传递给它的每个类型都可以为空(如果还没有).

//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;
        }

我是否需要明确检查字符串或我遗失了什么?

解决方法

告诉您某个值是特定类型还是从该特定类型派生的值.

Nullable是一个通用结构,允许可空值的非可空值.

string不是Nullable

要判断某个类型是否具有空值,请使用以下事实:对于所有此类类型,默认值为null,而对于所有其他类型,它不是:

default(string) == null; // true
原文链接:https://www.f2er.com/csharp/100316.html

猜你在找的C#相关文章