【问题标题】:nullable var using implicit typing in c#?在 c# 中使用隐式类型的可空变量?
【发布时间】:2009-02-11 15:54:44
【问题描述】:

无论如何让 var 是可以为空的类型?

这隐含地将 i 类型为 int,但如果我想要一个可为空的 int 怎么办?

var i = 0;

为什么不支持这个:

var? i = 0;

【问题讨论】:

  • 您的问题是“我如何在 C# 中做到这一点?”或“为什么 C# 不允许这样做?”第一个我们可以回答,但第二个属于微软(我们只能推测)。

标签: c# .net-3.5 nullable var


【解决方案1】:

var 由赋值右侧的表达式或常量隐式键入。 var 本身不是一个类型,所以 Nullable<var> 是不可能的。

【讨论】:

  • 是的,但他们可以做一些经典的语法糖魔法,其中var? i = 0 编译成var i = (int?)0,所以对我们来说更容易输入,并且应该很容易为他们实现。比完全重做 var 使其成为一个类型更容易
【解决方案2】:

为什么支持它?如果这就是你的意思,你应该改用var i = (int?)0;

(好吧,你可能应该直接说int? i = 0,但对于更复杂的类型等)

【讨论】:

    【解决方案3】:

    问题涉及可空类型。

    例如,您不能创建可为空的字符串,这反过来又会阻止您创建可为空的 var,因为 var 可能是字符串。

    【讨论】:

      【解决方案4】:

      我的回答是这样的。 “var”是隐式类型的。它通过赋值右侧提供的值确定它是什么类型。如果你告诉它它是可空的,它不知道是什么类型。请记住,一旦分配,它将永远是这种类型。

      【讨论】:

        【解决方案5】:

        试试这个 - 你说的是这个吗?

            static void Main(string[] args)
            {
                for (int i=0; i < 10; i++)
                {
                    var j = testMethod();
                    if (j == null)
                    {
                        System.Diagnostics.Debug.WriteLine("j is null");
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine(j.GetType().ToString());
                    }
                }
            }
        
            static int? testMethod()
            {
                int rem;
                Math.DivRem(Convert.ToInt32(DateTime.Now.Millisecond), 2, out rem);
                if (rem > 0)
                {
                    return rem;
                }
                else
                {
                    return null;
                }
            }
        

        【讨论】:

          【解决方案6】:

          VAR 是编译器在编译时确定的隐式类型。它是在首次使用时分配的。 IE 当你设置 var I = 1 时,var 是一个 int,因为这就是数字 1 的含义。如果你有 var I = SomeFunction() ,其中某个函数返回一个可为空的 int ,而不是 I 将被设置为 int?。现在,如果您想控制变量类型是什么,则不应使用上述 var。

          因此,根据您的示例,使用 var 是错误的,应该明确设置为 int?从一开始。

          • 达罗尔

          【讨论】:

            【解决方案7】:

            在这种情况下,我什至不会使用 var。如果它是复杂类型或简单类型,例如 int,我会使用类型“object”。

            例子:

            //Define all global class variables
            List<int> m_lInts = New List<int>();
            List<string> m_lStrs = New List<string>();
            
            public static object FindMyObject(string sSearchCritera)
            {
              //Initialize the variable
              object result = null;
            
              if (result == null)
              {
                //Search the ints list
                foreach(int i in m_lInts)
                {
                  if (i.ToString() == sSearchCritera)
                  {
                    //Give it a value
                    result = (int)i;
                  }
                }
              }
            
              if (result == null)
              {
                //Search the ints list
                foreach(string s in m_lStrs)
                {
                  if (s == sSearchCritera)
                  {
                    //Give it a value
                    result = (string)s;
                  }
                }
              }
            
              //Return the results
              return result;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2010-10-13
              • 2012-12-13
              • 1970-01-01
              • 1970-01-01
              • 2011-10-06
              • 2012-04-27
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多