【问题标题】:How to set null value to int in c#?如何在 C# 中将 null 值设置为 int?
【发布时间】:2013-10-31 13:55:55
【问题描述】:
int value=0;

if (value == 0)
{
    value = null;
}

如何将value 设置为上面的null

任何帮助将不胜感激。

【问题讨论】:

标签: c# .net null int nullable


【解决方案1】:

此外,您不能在条件赋值中使用“null”作为值。比如……

bool testvalue = false;
int? myint = (testvalue == true) ? 1234 : null;

失败:Type of conditional expression cannot be determined because there is no implicit conversion between 'int' and '<null>'.

因此,您也必须强制转换 null... 这有效:

int? myint = (testvalue == true) ? 1234 : (int?)null;

更新(2021 年 10 月):

从 C# 9.0 开始,您可以使用“Target-Typed”条件表达式,并且该示例现在可以工作,因为 c# 9 可以通过在编译时评估表达式来预先确定结果类型。

【讨论】:

  • 优秀。解决了我今晚遇到的一个问题。
  • 好吧,严格来说,这不是问题的答案,但我觉得它真的很有用。有人知道这种行为的原因吗?
  • @Cirelli94 三元运算符?: 尝试推断结果的类型。第一部分得到int,第二部分得到null。它不能隐含地解决这两个问题。但是,当将 null 转换为 Nullable<int>(简写 int?)时,会找到 intint? 的隐式转换并将其用于最终赋值。
【解决方案2】:
int ? index = null;

public int Index
        {
            get
            {
                if (index.HasValue) // Check for value
                    return index.Value; //Return value if index is not "null"
                else return 777; // If value is "null" return 777 or any other value
            }
            set { index = value; }
        }

【讨论】:

    【解决方案3】:
     public static int? Timesaday { get; set; } = null;
    

     public static Nullable<int> Timesaday { get; set; }
    

     public static int? Timesaday = null;
    

     public static int? Timesaday
    

    或者只是

     public static int? Timesaday { get; set; } 
    
    
        static void Main(string[] args)
        {
    
    
        Console.WriteLine(Timesaday == null);
    
         //you also can check using 
         Console.WriteLine(Timesaday.HasValue);
    
            Console.ReadKey();
        }
    

    null 关键字是表示空引用的文字,它不引用任何对象。 在编程中,可空类型是某些编程语言类型系统的一个特性,它允许将值设置为特殊值 NULL,而不是数据类型的通常可能值。

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/null https://en.wikipedia.org/wiki/Null

    【讨论】:

      【解决方案4】:

      int不允许为null,使用-

      int? value = 0  
      

      或使用

      Nullable<int> value
      

      【讨论】:

        【解决方案5】:

        声明你的整型变量可以为空 例如:int? variable=0; variable=null;

        【讨论】:

          【解决方案6】:

          您不能将int 设置为null。请改用可为空的 int (int?):

          int? value = null;
          

          【讨论】:

            【解决方案7】:

            在 .Net 中,您不能将 null 值分配给 int 或任何其他结构。请改为使用Nullable&lt;int&gt;,或简称为int?

            int? value = 0;
            
            if (value == 0)
            {
                value = null;
            }
            

            进一步阅读

            【讨论】:

            • 我试图从即时窗口分配 null 以进行调试。但它没有用。如何做到这一点?
            • @mahemadhi 可能出于同样的原因,您不能在常规代码中执行此操作。如果一个变量被声明为int,它不能接收null 作为值,即使在调试时也是如此。您需要将其声明为int?
            • 变量声明为nullable。然而在即时窗口中,我无法分配 null。
            猜你喜欢
            • 2016-06-01
            • 2014-02-03
            • 2014-03-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-03-08
            • 2021-03-13
            • 2018-09-25
            相关资源
            最近更新 更多