简体   繁体   English

声明,实例化,初始化和分配对象的含义

[英]Meanings of declaring, instantiating, initializing and assigning an object

Technically what are the meanings and differences of the terms declaring , instantiating , initializing and assigning an object in C#? 从技术上讲,在C#中声明实例化初始化分配对象的术语的含义和区别是什么?

I think I know the meaning of assigning but I have no formal definition. 我想我知道分配的意思,但我没有正式的定义。

In msdn, it is said "the act of creating an object is called instantiation". 在msdn中,有人说“创建对象的行为称为实例化”。 But the meaning creating seems vague to me. 创造的意义对我来说似乎含糊不清。 You can write 你可以写

int a;

is a then created? a然后创建?

Declaring - Declaring a variable means to introduce a new variable to the program. 声明 - 声明变量意味着向程序引入新变量。 You define its type and its name. 您可以定义其类型和名称。

int a; //a is declared

Instantiate - Instantiating a class means to create a new instance of the class. 实例化 - 实例化类意味着创建类的新实例。 Source . 来源

MyObject x = new MyObject(); //we are making a new instance of the class MyObject

Initialize - To initialize a variable means to assign it an initial value. 初始化 - 初始化变量意味着为其分配初始值。

int a; //a is declared
int a = 0; //a is declared AND initialized to 0
MyObject x = new MyObject(); //x is declared and initialized with an instance of MyObject

Assigning - Assigning to a variable means to provide the variable with a value. 分配 - 分配给变量意味着为变量提供值。

int a = 0; //we are assigning to a; yes, initializing a variable means you assign it a value, so they do overlap!
a = 1; //we are assigning to a

In general: 一般来说:

Declare means to tell the compiler that something exists, so that space may be allocated for it. 声明意味着告诉编译器存在某些东西,以便可以为它分配空间。 This is separate from defining or initializing something in that it does not necessarily say what "value" the thing has, only that it exists. 这与定义或初始化某些东西是分开的,因为它不一定表示该东西具有什么“价值”,只是它存在。 In C/C++ there is a strong distinction between declaring and defining. 在C / C ++中,声明和定义之间存在很大的区别。 In C# there is much less of a distinction, though the terms can still be used similarly. 在C#中,虽然术语仍可以类似地使用,但它的区别要小得多。

Instantiate literally means "to create an instance of". 实例化字面意思是“创建一个实例”。 In programming, this generally means to create an instance of an object (generally on "the heap"). 在编程中,这通常意味着创建对象的实例(通常在“堆”上)。 This is done via the new keyword in most languages. 这是通过大多数语言中的new关键字完成的。 ie: new object(); 即: new object(); . Most of the time you will also save a reference to the object. 大多数情况下,您还将保存对象的引用。 ie: object myObject = new object(); 即: object myObject = new object(); .

Initialize means to give an initial value to. 初始化意味着给出初始值。 In some languages, if you don't initialize a variable it will have arbitrary (dirty/garbage) data in it. 在某些语言中,如果不初始化变量,则其中将包含任意(脏/垃圾)数据。 In C# it is actually a compile-time error to read from an uninitialized variable. 在C#中,从未初始化的变量读取实际上是编译时错误。

Assigning is simply the storing of one value to a variable. 分配只是将一个值存储到变量中。 x = 5 assigns the value 5 to the variable x . x = 5将值5给变量x In some languages, assignment cannot be combined with declaration, but in C# it can be: int x = 5; 在某些语言中,赋值不能与声明组合,但在C#中它可以是: int x = 5; .

Note that the statement object myObject = new object(); 注意语句object myObject = new object(); combines all four of these. 结合了所有这四个。

  • new object() instantiates a new object object, returning a reference to it. new object()实例化一个新的object对象,返回对它的引用。
  • object myObject declares a new object reference. object myObject声明一个新的object引用。
  • = initializes the reference variable by assigning the value of the reference to it. =通过为其指定引用值来初始化引用变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM