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

    1. <small id='mopdD'></small><noframes id='mopdD'>

      1. <i id='mopdD'><tr id='mopdD'><dt id='mopdD'><q id='mopdD'><span id='mopdD'><b id='mopdD'><form id='mopdD'><ins id='mopdD'></ins><ul id='mopdD'></ul><sub id='mopdD'></sub></form><legend id='mopdD'></legend><bdo id='mopdD'><pre id='mopdD'><center id='mopdD'></center></pre></bdo></b><th id='mopdD'></th></span></q></dt></tr></i><div id='mopdD'><tfoot id='mopdD'></tfoot><dl id='mopdD'><fieldset id='mopdD'></fieldset></dl></div>
        • <bdo id='mopdD'></bdo><ul id='mopdD'></ul>
      2. 为什么必须初始化 C# 局部变量?

        Why C# local variables must be initialized?(为什么必须初始化 C# 局部变量?)

            <tbody id='ZYy62'></tbody>

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

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

              <bdo id='ZYy62'></bdo><ul id='ZYy62'></ul>

                <tfoot id='ZYy62'></tfoot>
                1. 本文介绍了为什么必须初始化 C# 局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在阅读 MCTS 自定进度培训套件 (70-536) 第 2 版,在第 1 章中我们有以下内容.

                  I am reading MCTS Self Paced Training Kit (70-536) Edition 2 and in the 1st chapter we have the following.

                  如何声明值类型变量要使用类型,您必须首先声明一个符号作为该类型的实例.值类型具有隐式构造函数,所以声明它们自动实例化类型;你不必包括新的关键字,就像您使用类一样.这构造函数分配一个默认值(通常为 null 或 0)到新的例如,但你应该总是显式初始化变量在声明中,如图所示以下代码块:

                  How to Declare a Value Type Variable To use a type, you must first declare a symbol as an instance of that type. Value types have an implicit constructor, so declaring them instantiates the type automatically; you don’t have to include the New keyword as you do with classes. The constructor assigns a default value (usually null or 0) to the new instance, but you should always explicitly initialize the variable within the declaration, as shown in the following code block:

                  'VB
                  
                  Dim b As Boolean = False    
                  
                  // C#  
                  bool b = false;
                  

                  但是,当我编译以下控制台应用程序时,

                  However, when I compile the following Console Application,

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Text;
                  
                  namespace Ch1_70_536
                  {
                      class Program
                      {
                          static void Main(string[] args)
                          {
                              bool b;
                              Console.WriteLine("The value of b is " + b);
                              Console.ReadKey();
                          }
                      }
                  }
                  

                  我得到编译时错误

                  未赋值局部变量 b 的使用"

                  "Use of Unassigned Local Variable b"

                  勘误表中甚至都没有提及.是我做错了什么还是这本书完全错了?

                  It is not even mentioned in the Errata. Am I doing something wrong or is the book completely wrong?

                  推荐答案

                  这本书在谈到 VB 时大部分是正确的,但在这种情况下没有提到 VB 和 C# 之间的区别.

                  The book is mostly correct when it comes to VB, but it fails to mention the difference between VB and C# in this case.

                  在 VB 中,所有局部变量都会自动初始化:

                  In VB all local variables are automatically initialised:

                  Sub Test()
                    Dim x As Integer
                    MessageBox.Show(x.ToString()) 'shows "0"
                  End Sub
                  

                  虽然在 C# 中局部变量未初始化,但编译器不会让您使用它们,直到它们:

                  While in C# local variables are not initialised, and the compiler won't let you use them until they are:

                  void Test() {
                    int x;
                    MessageBox.Show(x.ToString()); // gives a compiler error
                  }
                  

                  此外,还不清楚书中的引用实际上是在谈论局部变量还是类成员变量.在 VB 和 C# 中,类成员变量总是在创建类实例时初始化.

                  Also, it's not clear whether the quote from the book is actually talking about local variables or class member variables. Class member variables are always initialised when the class instance is created, both in VB and C#.

                  这本书说值类型有一个隐式构造函数"是错误的.那明显是错的.值类型被初始化为其默认值(如果已初始化),并且在这种情况下不会调用构造函数.

                  The book is wrong when it says that "Value types have an implicit constructor". That is simply not true. A value type is initialised to its default value (if it's initialised), and there is no call to a constructor when that happens.

                  这篇关于为什么必须初始化 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() 和泛型:错误绑定到目标方法)
                  <i id='ge8Me'><tr id='ge8Me'><dt id='ge8Me'><q id='ge8Me'><span id='ge8Me'><b id='ge8Me'><form id='ge8Me'><ins id='ge8Me'></ins><ul id='ge8Me'></ul><sub id='ge8Me'></sub></form><legend id='ge8Me'></legend><bdo id='ge8Me'><pre id='ge8Me'><center id='ge8Me'></center></pre></bdo></b><th id='ge8Me'></th></span></q></dt></tr></i><div id='ge8Me'><tfoot id='ge8Me'></tfoot><dl id='ge8Me'><fieldset id='ge8Me'></fieldset></dl></div>

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

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

                          <tfoot id='ge8Me'></tfoot><legend id='ge8Me'><style id='ge8Me'><dir id='ge8Me'><q id='ge8Me'></q></dir></style></legend>
                            <tbody id='ge8Me'></tbody>