简体   繁体   English

为什么C#动态类型是静态的?

[英]Why is C# dynamic type static?

While reading and exploring the dynamic keyword I found following line on [MSDN] (in Using Type dynamic (C# Programming Guide) ): 在阅读和探索我在[MSDN]上的以下行中找到的动态关键字时(在使用类型动态(C#编程指南)中 ):

The type is a static type, but an object of type dynamic bypasses static type checking. 类型是静态类型,但动态类型的对象绕过静态类型检查。 In most cases, it functions like it has type object. 在大多数情况下,它的功能类似于它具有类型对象。

What is the meaning of static in above line and how does it bypass static type checking? 静态在上面的行是什么意思,它如何绕过静态类型检查?

This is static typing: 这是静态类型:

string foo = "bar";

foo is now a string, so this will cause a compile time error: foo现在是一个字符串,因此这将导致编译时错误:

foo = 1;

Even if you use var , it's still statically typed: 即使你使用var ,它仍然是静态类型的:

var foo = "bar";     // foo is now a string
foo = 1;             // still a compile time error

Using the dynamic keyword, means the type won't be static and can be changed, so now you can do this: 使用dynamic关键字意味着类型不是静态的并且可以更改,所以现在你可以这样做:

dynamic foo = "bar";   
foo = 1;              // this is now fine.

Now, why it says "the type is a static type" is because in many dynamic languages (like Javascript), you can do something like this: 现在,为什么它说“类型是静态类型”是因为在许多动态语言(如Javascript)中,你可以这样做:

var foo = { bar: 1 };

Which creates an object with a property called "bar", and then you can do this: 这会创建一个名为“bar”的对象,然后你可以这样做:

foo.la = 2;

Which adds a new property to the object in foo . 这为foo的对象添加了一个新属性。 But if you try the same trick in C# 但是如果你在C#中尝试相同的技巧

dynamic foo = new SomeClassThatDoesntHaveABarProperty();
foo.bar = 2;          // runtime error

Nor can you delete a property. 你也不能删除一个属性。 You can assign any type to a dynamic variable, but you can't change those types themselves. 您可以将任何类型分配给动态变量,但不能自行更改这些类型。

If you do need that type of functionality, then you'll want to look at ExpandoObject 如果您确实需要这种类型的功能,那么您将需要查看ExpandoObject

As your description says, dynamic functions like an object in a lot of cases. 正如您的描述所说, dynamic函数在很多情况下都像对象一样。 You could do this: 你可以这样做:

dynamic foo = new Foo();
foo = new Bar();

Just as well like this: 就像这样:

object foo = new Foo();
foo = new Bar();

But the difference comes in when you want to use properties or methods. 但是,当您想要使用属性或方法时,会出现差异。 With dynamic, I can do this: 有了动态,我可以这样做:

dynamic foo = new Foo();
foo.FooMethod();          // Note: You WILL get a runtime exception if foo doesn't have a FooMethod

But with an object, I'd need to do this: 但是有了一个对象,我需要这样做:

object foo = new Foo();
((Foo)foo).FooMethod();    // Note: I HAVE to know the type at compile time here

Which I can only do if I already know I can cast the type in foo to a type of Foo at compile time , and if I knew that already, then I could just have use Foo as my type instead of object . 我只能知道我可以在编译时将 foo中的类型转换为类型的Foo ,如果我已经知道了,那么我可以使用Foo作为我的类型而不是object

It means that a variable declared as dynamic will stay of type dynamic and cannot be changed to a variable of type int for example. 这意味着声明为动态的变量将保持动态类型,并且不能更改为int类型的变量。 This concept is bypassed though, because you can change the types of objects that the variable holds. 但是,这个概念被绕过,因为您可以更改变量所包含的对象类型。

C# is considered as a strongly typed language because variables are statically typed. C#被认为是强类型语言,因为变量是静态类型的。 That means every variable is typed and the C#-compiler can check if the right types are used in the code. 这意味着每个变量都是类型化的,C#编译器可以检查代码中是否使用了正确的类型。 In weakly typed language like most script languages the type of variables is dynamic. 在像大多数脚本语言这样的弱类型语言中,变量的类型是动态的。 They can hold any value. 他们可以保持任何价值。

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

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