C#中变量和对象有什么区别? [英] What is the difference between variable and object in C# ?

查看:106
本文介绍了C#中变量和对象有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我有一个班级建筑

let's say that i have a Class Building

Building house = new Building();





现在我正在读一本书,上面写着



now i was reading in a book that say that

Quote:

这个声明执行三个功能。首先,它声明了一个名为House of

类类型Building的变量。这个变量本身不是一个对象。相反,它只是一个变量,

可以引用一个对象。

This declaration performs three functions. First, it declares a variable called house of the
class type Building. This variable is not, itself, an object. Instead, it is simply a variable that
can refer to an object.





然后我遇到了一个Answerin Stackoverflow说是那个



and then i am came across a Answerin Stackoverflow that say's that

引用:

如果你看一个例子,他很容易理解。例如,假设您有一个House House。你自己的房子是一个对象,是House之类的一个例子。你姐姐的房子是另一个对象(House的另一个例子

his is easy to understand if you look at an example. For example, suppose you have a class House. Your own house is an object and is an instance of class House. Your sister's house is another object (another instance of class House







// Class House describes what a house is
class House {
    // ...
}

// You can use class House to create objects (instances of class House)
House myHouse = new House();
House sistersHouse = new House();



现在我很困惑它是一个物体还是变量?

ps:我是C#的新手




我尝试了什么:



阅读了很多文章但很困惑


now i am confused whether it's an object or an Variable?
ps: i am new to C#


What I have tried:

read many articles but confused

推荐答案

变量只是类型的名称,我们基本上在
/library/view/programming-c-40/9781449392192/ch04s08.html\">c#一切都是对象
[ ^ ](所有类型都来自对象



简单单词,用于值类型 [ ^ ]我们将其称为变量 [ ^ ]其中参考类型 [ ^ ]我们称之为对象 [ ^ ]



例如:

Variable is just a name given to the type which we create
basically in c# everything is an object[^] (all the types are derived from object )

In simple words, For Value types[^] we will call it as variables [^] where as for reference type[^] we call it as object [^]

example:
 int i; 
//  i is a variable name for the  value type  int




MyClass objMyClass ;
 //objMyClass is an object(variable name) of type MyClass which is a  reference type 


Google搜索: c#variable vs object [ ^ ]有这样的宝石: C#中的价值与参考类型 [ ^ ]
Google Search: c# variable vs object[^] had this gem: Value vs Reference Types in C#[^]


这篇关于C#中变量和对象有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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