声明,实例化,初始化和分配对象的含义 [英] Meanings of declaring, instantiating, initializing and assigning an object

查看:83
本文介绍了声明,实例化,初始化和分配对象的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从技术上讲,声明实例化初始化分配这些术语的含义和区别是什么在C#中?

Technically what are the meanings and differences of the terms declaring, instantiating, initializing and assigning an object in C#?

我想我知道赋值的含义,但我没有正式定义。

I think I know the meaning of assigning but I have no formal definition.

在msdn中,据说创建对象的行为称为实例化。但是 creating 的含义对我来说似乎很模糊。您可以写

In msdn, it is said "the act of creating an object is called instantiation". But the meaning creating seems vague to me. You can write

int a;

然后创建 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

这篇关于声明,实例化,初始化和分配对象的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆