C# 变量初始化与赋值

发布于 2024-09-24 12:09:42 字数 307 浏览 2 评论 0原文

我在一本书中发现以下内容(翻译):

初始化是指在声明时为变量赋值。 int X=5称为初始化命令。

编辑:它只是说仅当您在声明时赋值时才使用术语初始化。如果你稍后再做,它只是作业(根据它 - 我不这么认为,这就是我问的原因)。到底是真是假?

嗯,我一直认为(也根据网上其他人的说法)关于初始化是指第一次为变量赋值。我认为 int X=5 只是作为声明一部分的赋值。

我尝试在 MSDN 上搜索,但没有成功。感谢您提供任何信息。

In a book I found following (translation):

Initialization means assigning the value of the variable at the declaration time. int X=5 is called an initialization command.

EDIT: It just says that term initialization is used only when you assign value at the declaration time. If you do it later, its just assignement (according to it - I do not think so and that is why I am asking). Is it true or not?

Well, I have always thought (and according to others on the net, too) about initialization in the terms of first assignment of a value to a variable. I think that int X=5 is just assignment as a part of declaration.

I tried to search on MSDN with no luck. Thanks for any information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

又怨 2024-10-01 12:09:42

如果您问的只是术语(您的问题并不清楚),那么变量的“初始化”从字面上看就是第一次为其赋值。该术语来自这样一个事实:您为变量赋予了“初始”值。

这应该(显然)在第一次使用之前发生。

int x=5;

是一个声明和一个初始化,实际上只是方便的简写

int x;
x=5;

If all you are asking about is terminology (which isn't really clear from your question), "Initialization" of a variable is, literally, the first time a value is assigned to it. This term comes from the fact that you are giving the variable it's "initial" value.

This should (obviously) happen before it is used the first time.

int x=5;

is a declaration and an initialization, and is really just convenient shorthand for

int x;
x=5;
我是男神闪亮亮 2024-10-01 12:09:42

一般来说,变量的初始化是声明后的第一次赋值,因为变量不会自动初始化。后续的任务只是任务。

void foo() {
    int i;//not initialized.
}

除了 字段 之外,它们是在类或结构中声明的变量。在调用构造函数之前,它们会被初始化为其默认值该对象实例。因此,即使您(首先)将某些内容分配给构造函数中的字段并且该字段在声明时未初始化,严格来说它也是赋值,而不是初始化。在不讨论这些细节的情况下,我想许多程序员确实将构造函数中字段的第一次赋值视为初始化,并且可能在彼此的对话中使用它。这种用法/习惯可能源于使用像 C/C++ 这样的非托管语言,其中变量在首次声明时包含乱码,因此必须用非垃圾值填充它(初始化)。

class A {
    int x;//not initialized
    int y = 1;//initialized here
    A() {
        x = 1;//strictly speaking, not initialization, just assignment.
        y = 2;//was obviously initialized above
    }
}

对于不编写编译器或语言的程序员来说,能够与其他程序员有效地沟通比了解初始化一词的确切术语更重要。因此,请按照周围每个人都能理解的含义来使用它。不过,你解释确切的含义不会错(但我可能不会,因为担心暴露我的迂腐倾向)。

就我个人而言,我会避免阅读使用术语“初始化命令”来表示变量的书。

Generally, initialization of a variable is the first assignment after declaration, as variables are not automatically initialized. Subsequent assignments are just assignments.

void foo() {
    int i;//not initialized.
}

Except for fields, which are variables declared in a class or struct. These are initialized to their default values just before the constructor is called for that object instance. Hence, even if you (first) assign something to a field in a constructor and that field was not initialized at declaration, it is strictly speaking assignment, not initialization. Without going into such detail, I guess many programmers do consider the first assignment of a field in a constructor as initialization though, and may use it as such in their conversations with each other. Probably this usage/habit stems from having used unmanaged languages like C/C++ where a variable contains gibberish when first declared, and hence they have to fill it with non-rubbish values (initialization).

class A {
    int x;//not initialized
    int y = 1;//initialized here
    A() {
        x = 1;//strictly speaking, not initialization, just assignment.
        y = 2;//was obviously initialized above
    }
}

For programmers not writing compilers or languages, to be able to communicate effectively with other programmers is more important than knowing the exact terminology of the word initialization. So go ahead and use it in the meaning which everyone around you understand. You will not be wrong to explain the exact meaning though (but I probably won't for fear of exposing my pedantic tendencies).

Personally I will avoid a book that uses a term "initialization command" for variables.

九局 2024-10-01 12:09:42

一般来说,如果第一次分配变量,则初始化,无论是由程序显式分配还是由编译器/运行时隐式分配。然而,理解初始化机制很重要,尤其是在使用类时。考虑逐步完成下面的简单示例(在某处创建“Bar”的实例)以查看编译器如何进行初始化过程。您会注意到这些字段是通过初始化来声明的,并且在构造函数有机会设置它们之前所有字段都已“初始化”。

public class barBaseBase
{
    protected int x = 5;
    public barBaseBase()
    {
        x = 4;
    }
}

public class barBase : barBaseBase
{
    protected int y = 4;
    public barBase()
    {
        x = 3;
        y = 2;
    }
}

public class Bar : barBase
{
    protected int z = 3;
    public Bar()
    {
        x = 2;
        y = 1;
        z = 0;
    }
}

In general, initialization if the first time a variable is assigned to, whether explicitly by the program or implicitly by the compiler/run-time. However the mechanisms of initialization are important to understand especially when working with a classes. Consider stepping through the simple example below (Create an instance of 'Bar' somewhere) to see how the compiler goes about the process of initialization. You will notice that the fields are declared with initialization and all are "initialized" before the constructors have a chance to set them.

public class barBaseBase
{
    protected int x = 5;
    public barBaseBase()
    {
        x = 4;
    }
}

public class barBase : barBaseBase
{
    protected int y = 4;
    public barBase()
    {
        x = 3;
        y = 2;
    }
}

public class Bar : barBase
{
    protected int z = 3;
    public Bar()
    {
        x = 2;
        y = 1;
        z = 0;
    }
}
噩梦成真你也成魔 2024-10-01 12:09:42

请注意,初始化字段会导致 C# 编译器为每个构造函数发出初始化 IL 指令。因此,如果您有多个构造函数,则会出现重复的初始化 IL - 即不必要的代码膨胀。

最好在默认构造函数中初始化变量并让其他构造函数调用它。

Note that initializing fields causes the C# compiler to emit the initialization IL instructions for each constructor. So if you have multiple constructors, you'll have duplication of the initialization IL - i.e. unnecessary code bloat.

Better to initialize the variables in the default constructor and have the other constructors call that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文