• <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'>


        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>



                  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?)



                  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#.




                  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>