• <bdo id='lTgnZ'></bdo><ul id='lTgnZ'></ul>

        <i id='lTgnZ'><tr id='lTgnZ'><dt id='lTgnZ'><q id='lTgnZ'><span id='lTgnZ'><b id='lTgnZ'><form id='lTgnZ'><ins id='lTgnZ'></ins><ul id='lTgnZ'></ul><sub id='lTgnZ'></sub></form><legend id='lTgnZ'></legend><bdo id='lTgnZ'><pre id='lTgnZ'><center id='lTgnZ'></center></pre></bdo></b><th id='lTgnZ'></th></span></q></dt></tr></i><div id='lTgnZ'><tfoot id='lTgnZ'></tfoot><dl id='lTgnZ'><fieldset id='lTgnZ'></fieldset></dl></div>
        <legend id='lTgnZ'><style id='lTgnZ'><dir id='lTgnZ'><q id='lTgnZ'></q></dir></style></legend>
        <tfoot id='lTgnZ'></tfoot>

        <small id='lTgnZ'></small><noframes id='lTgnZ'>

        C#中的非初始化变量

        Noninitialized variable in C#(C#中的非初始化变量)

      1. <tfoot id='NThjL'></tfoot>
        <legend id='NThjL'><style id='NThjL'><dir id='NThjL'><q id='NThjL'></q></dir></style></legend>

          <i id='NThjL'><tr id='NThjL'><dt id='NThjL'><q id='NThjL'><span id='NThjL'><b id='NThjL'><form id='NThjL'><ins id='NThjL'></ins><ul id='NThjL'></ul><sub id='NThjL'></sub></form><legend id='NThjL'></legend><bdo id='NThjL'><pre id='NThjL'><center id='NThjL'></center></pre></bdo></b><th id='NThjL'></th></span></q></dt></tr></i><div id='NThjL'><tfoot id='NThjL'></tfoot><dl id='NThjL'><fieldset id='NThjL'></fieldset></dl></div>
            • <bdo id='NThjL'></bdo><ul id='NThjL'></ul>

                  <small id='NThjL'></small><noframes id='NThjL'>

                    <tbody id='NThjL'></tbody>
                  本文介绍了C#中的非初始化变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下代码:

                  class Foo
                  {
                  
                      public Foo()
                      {
                          Bar bar;
                          if (null == bar)
                          {
                  
                          }
                      }
                  }
                  
                  class Bar { }
                  

                  代码专家已经看到这会产生错误.Bar 可能不会在 if 语句之前初始化.

                  Code gurus will already see that this gives an error. Bar might not be initialized before the if statement.

                  什么是 bar 的值?它不应该为空吗?他们不是设置为null吗?(空指针?)

                  What is the value of bar? Shouldn't it be null? Aren't they set to null? (null pointer?)

                  推荐答案

                  不,局部变量没有默认值1.在您阅读它们之前,它们必须明确分配.这减少了您使用您认为您已赋予合理值的变量的机会,而实际上它具有一些默认值.对于实例变量或静态变量,这是无法做到的,因为您不知道调用方法的顺序.

                  No, local variables don't have a default value1. They have to be definitely assigned before you read them. This reduces the chance of you using a variable you think you've given a sensible value to, when actually it's got some default value. This can't be done for instance or static variables because you don't know in what order methods will be called.

                  有关明确赋值的更多详细信息,请参阅 C# 3.0 规范的第 5.3 节.

                  See section 5.3 of the C# 3.0 spec for more details of definite assignment.

                  请注意,这与 this 作为引用类型变量无关.这将无法以相同的方式编译:

                  Note that this has nothing to do with this being a reference type variable. This will fail to compile in the same way:

                  int i;
                  if (i == 0) // Nope, i isn't definitely assigned
                  {
                  }
                  

                  <小时>

                  1 就语言而言,无论如何...显然内存中的存储位置有 something 在其中,但它无关紧要且特定于实现.one 方法可以让您找出该值是什么,方法是创建一个带有 out 参数的方法,然后使用 IL 在方法中查看该参数的值,而没有给它另一个值.CLR 根本不介意这一点.然后,您可以调用该方法传入一个未明确分配的变量,然后您会发现您可以检测到该值 - 基本上可能是全零"值.


                  1 As far as the language is concerned, anyway... clearly the storage location in memory has something in it, but it's irrelevant and implementation-specific. There is one way you can find out what that value is, by creating a method with an out parameter but then using IL to look at the value of that parameter within the method, without having given it another value. The CLR doesn't mind that at all. You can then call that method passing in a not-definitely-assigned variable, and lo and behold you can detect the value - which is likely to be the "all zeroes" value basically.

                  我怀疑 CLI 规范确实强制执行具有默认值的局部变量 - 但我必须检查一下.除非你在做上述那样的坏事,否则在 C# 中对你来说应该没关系.

                  I suspect that the CLI specification does enforce local variables having a default value - but I'd have to check. Unless you're doing evil things like the above, it shouldn't matter to you in C#.

                  这篇关于C#中的非初始化变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                    <tbody id='Wagaa'></tbody>
                      <bdo id='Wagaa'></bdo><ul id='Wagaa'></ul>

                          <small id='Wagaa'></small><noframes id='Wagaa'>

                        1. <legend id='Wagaa'><style id='Wagaa'><dir id='Wagaa'><q id='Wagaa'></q></dir></style></legend>

                        2. <tfoot id='Wagaa'></tfoot>
                            <i id='Wagaa'><tr id='Wagaa'><dt id='Wagaa'><q id='Wagaa'><span id='Wagaa'><b id='Wagaa'><form id='Wagaa'><ins id='Wagaa'></ins><ul id='Wagaa'></ul><sub id='Wagaa'></sub></form><legend id='Wagaa'></legend><bdo id='Wagaa'><pre id='Wagaa'><center id='Wagaa'></center></pre></bdo></b><th id='Wagaa'></th></span></q></dt></tr></i><div id='Wagaa'><tfoot id='Wagaa'></tfoot><dl id='Wagaa'><fieldset id='Wagaa'></fieldset></dl></div>