我需要知道这三个关键字对象
,动态
和 VAR的区别
在C#。
I need to know the difference between these three keywords Object
, Dynamic
and var
in C#.
我已经看到了这链接,但我不明白,在这种情况下,我必须使用每一个。
I have seen this link but i don't understand in which case i have to use each one.
您可以给我解释一下,请,这些关键字的区别?什么是每个关键字的事业吗?
Can you explain for me, please, the difference between these keywords ? What are the utilities of each keyword?
一切都是对象
,每一个类型的对象继承了一下,一个简单的 INT
变量可以拳击到对象
。例如:
Everything is object
, every type inherit from Object in a moment, a simple int
variable can be boxing to an object
. For example:
object a = 10; // int
object b = new Customer(); // customer object
object c = new Product(); // product object
object d = "Jon"; // string
object e = new { Name = "Felipe", Age = 20 }; // anonymous type
有最抽象为任何类型,它是一个引用类型。如果你想获得真正的类型,则需要拆箱
是:
It is the most abstraction for any type and it is a reference type. If you want to get the real type, you need to unbox
it:
object a = "Some Text";
string a1 = a.ToString();
// call a string method
a1.ToUpper();
object i = 10; // declared as object but instance of int
int i2 = (int) i; //declare as an int ... typed
动态
是NA实现在C#中的动态方面,它不是强类型。例如:
Dynamic
is na implementation of a dynamic aspect in C#, it is not strongly typed. For example:
dynamic a = new Class();
a.Age = 18;
a.Name = "Jon";
a.Product = new Product();
a.Name; // read a string
a.Age; // read an int
a.Product.Name; // read a property
VAR
仅仅是C#语言的关键字,你可以因为你有一个类型,初始化它,例如创建任何类型:
var
is just a keyword of the C# language, you can create any type since you initialize it with a type, for example:
var a = 10; // int
var b = 10d; // double
var c = "text"; // string