为什么必须初始化 C# 局部变量?

Why C# local variables must be initialized?(为什么必须初始化 C# 局部变量?)
本文介绍了为什么必须初始化 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# 局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Is there a runtime benefit to using const local variables?(使用 const 局部变量有运行时的好处吗?)
How to avoid #39;Unassigned Local Variable#39; defined inside a try-catch block(如何避免在try-Catch块中定义未赋值的局部变量)
When does a local variable inside a function *actually* gets allocated(函数内的局部变量何时*实际*被分配)
How to remove compiler error with struct: quot;Use of unassigned local variablequot;(如何使用 struct 删除编译器错误:“使用未分配的局部变量)
Strange behavior with actions, local variables and garbage collection in MVVM light Messenger(MVVM light Messenger 中的动作、局部变量和垃圾收集的奇怪行为)
Why does this (null || !TryParse) conditional result in quot;use of unassigned local variablequot;?(为什么这个 (null || !TryParse) 条件会导致“使用未分配的局部变量?)