1. <tfoot id='HmJGo'></tfoot>

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

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

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

      为什么我必须在默认为 0 时为 C# 中的 int 赋值?

      Why do I have to assign a value to an int in C# when defaults to 0?(为什么我必须在默认为 0 时为 C# 中的 int 赋值?)
        • <bdo id='HuFB7'></bdo><ul id='HuFB7'></ul>

              <legend id='HuFB7'><style id='HuFB7'><dir id='HuFB7'><q id='HuFB7'></q></dir></style></legend><tfoot id='HuFB7'></tfoot>

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

              • <small id='HuFB7'></small><noframes id='HuFB7'>

                  <tbody id='HuFB7'></tbody>

                本文介绍了为什么我必须在默认为 0 时为 C# 中的 int 赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                这行得通:

                class MyClass
                {
                    int a;
                
                    public MyClass()
                    {
                        int b = a;
                    }
                }
                

                但这会导致编译器错误(使用未分配的局部变量'a'"):

                But this gives a compiler error ("Use of unassigned local variable 'a'"):

                class MyClass
                {
                    public MyClass()
                    {
                        int a;
                        int b = a;
                    }
                }
                

                据我所知,这是因为在第一个示例中,从技术上讲,编译器不知道没有分配a".在后一个示例中,a"是在本地定义的,因此很容易跟踪.

                As far as I can tell this happens because in the first example, technically, the compiler doesn't know that 'a' is not assigned. In the latter example, 'a' is defined locally, and therefore is easy to track.

                但为什么后一个例子不起作用?

                But why does the latter example not work?

                整数不默认为 0 吗?这是编译器为最佳实践"而强制执行的.还是有其他原因?

                Don't integers default to 0? Is this something the compiler enforces for "best practices". Or is there another reason?

                推荐答案

                在第一个示例中,它是一个字段.字段自动默认为 0/false/null.在第二个示例中,它是一个变量.变量不是默认的,在使用之前必须有明确的赋值".

                In the first example it is a field. Fields automatically default to 0/false/null. In the second example it is a variable. Variables are not defaulted, and must have "definite assignment" before they are used.

                本质上,在创建对象(或初始化结构)时,它会将内存归零(或者在非默认结构 ctor 的情况下,强制您手动初始化所有内容).但是,变量非常常见(在每种方法中),它不希望始终将堆栈归零的开销.相反,它会强制您指明初始值.

                Essentially, when creating an object (or initializing a struct) it zeros the memory (or in the case of a non-default struct ctor, forces you to manually initialize everything). However, variables are so common (in every method) that it doesn't want the overhead of having to zero the stack all the time. It instead forces you to indicate the initial value.

                这篇关于为什么我必须在默认为 0 时为 C# 中的 int 赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                相关文档推荐

                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() 和泛型:错误绑定到目标方法)

                  • <bdo id='Y3Kqb'></bdo><ul id='Y3Kqb'></ul>
                  • <legend id='Y3Kqb'><style id='Y3Kqb'><dir id='Y3Kqb'><q id='Y3Kqb'></q></dir></style></legend>

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

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