Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 最简单的检查方法是某个类型可为null(包括字符串)_C# - Fatal编程技术网

C# 最简单的检查方法是某个类型可为null(包括字符串)

C# 最简单的检查方法是某个类型可为null(包括字符串),c#,C#,检查某个类型是否可为null的最佳方法是什么 一些建议的代码无法测试其他类型,尤其是字符串。最简单的方法是: public static bool IsNullable(this Type type) { if (type.IsValueType) return Activator.CreateInstance(type) == null; return true; } 这些是我的单元测试,都通过了 IsNullable_Stri

检查某个类型是否可为null的最佳方法是什么


一些建议的代码无法测试其他类型,尤其是字符串。

最简单的方法是:

    public static bool IsNullable(this Type type)
    {
        if (type.IsValueType) return Activator.CreateInstance(type) == null;
        return true;
    }
这些是我的单元测试,都通过了

    IsNullable_String_ShouldReturn_True
    IsNullable_Boolean_ShouldReturn_False
    IsNullable_Enum_ShouldReturn_Fasle
    IsNullable_Nullable_ShouldReturn_True
    IsNullable_Class_ShouldReturn_True
    IsNullable_Decimal_ShouldReturn_False
    IsNullable_Byte_ShouldReturn_False
    IsNullable_KeyValuePair_ShouldReturn_False
实际单元测试

    [TestMethod]
    public void IsNullable_String_ShouldReturn_True()
    {
        var typ = typeof(string);
        var result = typ.IsNullable();
        Assert.IsTrue(result);
    }

    [TestMethod]
    public void IsNullable_Boolean_ShouldReturn_False()
    {
        var typ = typeof(bool);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_Enum_ShouldReturn_Fasle()
    {
        var typ = typeof(System.GenericUriParserOptions);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_Nullable_ShouldReturn_True()
    {
        var typ = typeof(Nullable<bool>);
        var result = typ.IsNullable();
        Assert.IsTrue(result);
    }

    [TestMethod]
    public void IsNullable_Class_ShouldReturn_True()
    {
        var typ = typeof(TestPerson);
        var result = typ.IsNullable();
        Assert.IsTrue(result);
    }

    [TestMethod]
    public void IsNullable_Decimal_ShouldReturn_False()
    {
        var typ = typeof(decimal);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_Byte_ShouldReturn_False()
    {
        var typ = typeof(byte);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }

    [TestMethod]
    public void IsNullable_KeyValuePair_ShouldReturn_False()
    {
        var typ = typeof(KeyValuePair<string, string>);
        var result = typ.IsNullable();
        Assert.IsFalse(result);
    }
[TestMethod]
public void IsNullable_String_ShouldReturn_True()
{
变量类型=类型(字符串);
var result=typ.IsNullable();
断言(结果);
}
[测试方法]
public void IsNullable_Boolean_ShouldReturn_False()
{
变量类型=类型(布尔);
var result=typ.IsNullable();
Assert.IsFalse(结果);
}
[测试方法]
public void可为null_Enum_ShouldReturn_Fasle()
{
var typ=typeof(System.GenericUriParserOptions);
var result=typ.IsNullable();
Assert.IsFalse(结果);
}
[测试方法]
public void IsNullable_Nullable_shouldlreturn_True()
{
变量类型=类型(可为空);
var result=typ.IsNullable();
断言(结果);
}
[测试方法]
public void可为空\u类\u应返回\u True()
{
变量类型=类型(测试人员);
var result=typ.IsNullable();
断言(结果);
}
[测试方法]
public void可为空\u Decimal\u ShouldReturn\u False()
{
变量类型=类型(十进制);
var result=typ.IsNullable();
Assert.IsFalse(结果);
}
[测试方法]
public void IsNullable_Byte_ShouldReturn_False()
{
变量类型=类型(字节);
var result=typ.IsNullable();
Assert.IsFalse(结果);
}
[测试方法]
public void可为空\u KeyValuePair\u应返回\u False()
{
变量类型=类型(KeyValuePair);
var result=typ.IsNullable();
Assert.IsFalse(结果);
}

您回答了自己的问题?通常允许回答自己的问题。但在这种情况下,他将自己的答案带到了另一个问题上,并创建了一个新的重复问题,只是为了再次发布完全相同的答案。所以在这里投反对票是完全值得的。这也是我的答案——VJPPaz,我不知道转发答案是个坏主意。我应该从中吸取教训是的,这很好,但它是重复的。已经问过了,回答过了。在你问一个新问题之前,一定要检查是否有重复的问题。事实上,这是在他已经把答案贴在上面之后创建的。好吧,这就是当你试图通过游戏系统获得一些微不足道的分数时会发生的事。它很快就被发现了,你失去的远远超过你得到的。对不起,我不知道我会因此失去一些分数。注意,hehe
string
是一个引用类型,因此定义为空。如果您试图检查typeof(string),则此代码不起作用
!certainType.IsValueType || Nullable.GetUnderlyingType(certainType) != null