简体   繁体   English

如何检查c#中是否将值分配给int?

[英]How do I check if a value is assigned to an int in c#?

int x might not have an assignment. int x可能没有分配。 How do I detect that? 如何检测到? What value does int hold when it is unassigned? 取消分配int时,它保持什么值?

This gives me a warning in Visual Studio: 这给我在Visual Studio中的警告:

public SomeMethod(int x) {
    if (x == null) {
    /* VS throws a warning:
    'The result of the expression is always 'false' since a value of type 'int' 
     is never equal to 'null' of type 'int' */
    };
}

This also gives me a warning in Visual Studio: 这也给我在Visual Studio中的警告:

public SomeMethod(int? x) { 
    // declaring int 'Nullable' does not fix the warning.
    if (x == null) {
    // Same warning.
    };
}

Likewise: 同样地:

public SomeMethod(int? x) {
    if (!x.HasValue) { 
    //  This does not work either.
    };
}

int x might not have an assignment. int x可能没有分配。

That's simply not true. 那根本不是真的。 In your case, x is a parameter - it's "definitely assigned" from the start of the method, in specification terminology. 在您的情况下, x是一个参数-在规范术语中,它是从方法开始时“确定分配”的。 When someone calls the method, they have to provide a value. 当有人调用该方法时,他们必须提供一个值。 There's no concept of the value not being assigned. 没有未分配值的概念。

If you want to represent a parameter which may not have a meaningful int value, you should use Nullable<int> , aka int? 如果要表示一个可能没有有意义的int值的参数,则应使用Nullable<int> ,也称为int? :

public void DoSomething(int? x)
{
    if (x == null)
    {
        ...
    }
    else
    {
        // Or whatever
        Console.WriteLine(x.Value);
    }
}

As of C# 4, you could then give the parameter a default value of null if you want to be able to call it as just foo.DoSomething() without providing a value. 从C#4开始,如果希望仅作为foo.DoSomething()调用而不提供值,则可以将参数的默认值设置为null You couldn't distinguish between that and foo.DoSomething(null) though. 你不能区分那个和foo.DoSomething(null)

In your case the best solution probably would be to use a default value for the parameter like this, so that when the parameter is null, the code does not throw an error and also the compiler will not show any warning - 在您的情况下,最好的解决方案可能是对像这样的参数使用默认值,以便当参数为null时,代码不会引发错误,并且编译器也不会显示任何警告-

    public void test(int? x){
        if (x == null)
        {
            Console.Write("test");
        }
    }

    public void testA(int? x = null)
    {
        if (x == null)
        {
            Console.Write("test");
        }
    }

    static void Main(string[] args)
    {
        var p = new Program();
        p.test(null);
        p.testA();
    }

But the code - 但是代码-

public SomeMethod(int x) {
    if (x == null) {
    /* Never happens because 'The result of the expression is always 'false' since a value of type 'int' is never equal to 'null' of type 'int' */
    };
}

will always show warning because the default value for int is 0 , not null as mentioned here - 将始终显示警告,因为默认值int0 ,而不是null这里提到-

C# Primitive Types C#基本类型

And thus the the value for x == null is always false. 因此, x == null值始终为false。

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

相关问题 当它说分配给它的值从未在 C# 中读取时,我该如何解决? - How do I fix when it says value assigned to it is never read in C#? 我如何使用C#检查数据源的正常运行时间值 - How do I check is uptime value on datasource with C# 如何检查C#中未选择ListBox的选定值? - How do I check if selected value of the ListBox is not selected in C#? 如何将布尔值转换为int并将其保存到C#中的MySQL表中? - How do I convert a boolean value to an int and save it to a MySQL table in C#? 如何在 c# 中找到随机生成的 int 的最后一个值? - How do I find the last value of a randomly generated int in c#? 如何在 C# 中将“Keys”枚举值转换为“int”字符? - How do I convert a "Keys" enum value to an "int" character in C#? C#-如何通过比较我的int值在组合框中设置所选项目? - C# - How do I set the selected item in a combobox by comparing my int value? 如何在C#中从程序设置或INI(自定义类)中将十六进制值作为int加载? - How do I load hex value from program settings or INI (custom class) in C# as an int? 使用C#插入值时,如何检查数据库中是否已存在值 - How do I check if value already exists in the database when I insert values using C# 如何将 int 值表示为 char/string (C#)? - How can I represent an int value as a char/string (C#)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM