var和Object之间的区别 [英] Difference between var and Object

查看:255
本文介绍了var和Object之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Var与Object有何不同. Object是一个泛型类,所有其他类,例如string,int32等,都是从该类派生的.但是我听不懂var,我已经读了很多文章.请清除我的概念.

How Var is different from Object. Object is a generic class, all the other classes say string, int32 etc are derived from it. but i cannot understand var, i''ve read number of articles. please clear my concept. thanks in adnvance.

推荐答案

我认为这将消除您的疑虑.
http://www.interact-sw.co.uk/iangblog/2005/09/23/varisntobject [ ^ ]
I think this will clear your doubts.
http://www.interact-sw.co.uk/iangblog/2005/09/23/varisntobject[^]


首先,System.Object根本不是通用类.它是普通"非泛型类,是所有类型的基类.

现在,任何类都是类型,但是var根本不是类型. If是用于类型推断的关键字.参见:
http://en.wikipedia.org/wiki/Type_inference [ http://msdn.microsoft.com/en-us/library/ms364047%28v = vs.80%29.aspx [ ^ ].

最简单的例子:

First, System.Object is not a generic class at all. It is a "normal" non-generic class, a base class of all types.

Now, any class is a type, but var is not a type at all. If is a keyword used for type inference. See:
http://en.wikipedia.org/wiki/Type_inference[^],
http://msdn.microsoft.com/en-us/library/ms364047%28v=vs.80%29.aspx[^].

Simplest example:

var reader = System.Io.StreamReader(fileName);



像这样声明reader时,编译器使用的名称与声明为System.Io.StreamReader时的名称完全相同.为什么?因为编译器可以弄清楚没有其他选择.在此示例中,var的工作方式类似于不需要命名的类型",您知道的类型".编译器可以在更困难的情况下推断名称.例如,它可以是一个函数,而不是构造函数,而应是具有相同名称的几个函数之一:



When reader is declared like that, a compiler works with this name exactly as it was declared as System.Io.StreamReader. Why? Because a compiler can figure out that there is no other options. In this example, var works like "type-that-need-not-to-be-named", "type-you-know-which". A compiler can infer the name in much more difficult situations. For example, instead of constructor it could be a function, one of several functions with the same name:

string RightSideOperand(bool isEmpty) { if (isEmpty) return string.Empty; else return "Sample string"; }
double RightSideOperand() { return 3.14159; }

var stringVariable = RightSideOperand(true); // stringVariable type is inferred as string
var doubleVariable = RightSideOperand(); // stringVariable type is inferred as double



甚至还有更复杂的情况,其中许多与var不相关.



现在,有关您的问题的正确性的一些建议.问题"{0}和{1}有什么区别?"不正确,您的问题很好地说明了这种错误.如果看到我的回答,您就可以理解.

我在过去的答案中对此进行了解释:什么是编程中类和封装之间的区别 [如何提出好问题? [ ^ ].

最好的祝福,

—SA



There are even more complex cases, and many of them are not related to var.



Now, some advice about correctness of your questions. The questions "What is the difference between {0} and {1}?" cannot be correct, and your question is a good illustration of such incorrectness. You can understand it if you see my answer.

I explained it in my past answer: what is the difference between the class and encapsulation in programming[^].

Please see the discussion about good and bad questions: How to ask a good question?[^].

Best wishes,

—SA


知道在不同情况下使用哪种数据类型通常是一个基本级别的决定.
var是一个很好的变量,因为您无需决定使用System.String还是System.Int32,因为将值与var关联时,编译器将为您确定类型.
IMP
To know which data type to use in different situations is generally a basic level decision.
The var is a nice one because you do not need to decide whether to use a System.String or System.Int32 becuase when you associate a value to the var, the compiler will determine the type for you.
IMP
报价:

[使用var不会消除理解数据类型的内存使用,存储使用,精度等的责任.它只是使事情变得更加轻松和快捷,但请谨慎使用.]

[using var does not remove the responsibility from understanding the memory usage, storage usage, percision, etc..of the data type. It''s just makes things a bit eaiser and faster, but use it with caution.]




使用var,您将可以访问数据类型的类的属性和成员.对于对象而言,情况并非如此.在编译时还不知道对象内存储的值是什么数据类型.您可以将类或通用列表存储在对象中. IE.引用和值类型都可以存储在一个对象中.
假设您有一个名为Person
的简单类




With var you will get access to the the data type class properties and members The same is not true for an object. It is not known at compile time what data type the value stored within the object is. You can store classes or generic lists in an object. I.e. both reference and value types can be stored in an object.
Let say you have a simple class named Person

public class Person
{
    public string Name { get; set; }
    public string Age { get; set; }
}



如果使用var包含该类,则该类中的属性可用,但是如果使用object则不可用.

什么时候在C#中使用对象?在我们将收到数据列表但不确定其中数据类型的情况下,请使用对象变量.例如:



If we use var to contain the class, then the properties within the class are available, however if we use object they are not.

When to use an object in C#? Use object variables in situations when we will receive a list of data but you are not certain of the data types within them. For example:

List<object> fields =  new List<object>(BuildFieldList(Query));



上面标识的查询是用户从报告工具中选择的一列列.我们不确定用户选择了哪一列或有多少用户.因此,我们决定使用对象变量.

什么时候在C#中使用var?与上面的示例相同,我们将使用一个var来包含已执行查询的结果集.


希望对您有帮助



The Query identified above is a string of columns a user has selected from a reporting tool. We are not certain which column or how many the user has selected. Therefore we decide to use an object variable.

When to use a var in C#? Following along the same line as the above example, we would use a var to contain the result set from the Query that was executed.


I hope this is helpful


这篇关于var和Object之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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