为什么可以定义一个名为var或dynamic的类? [英] Why that is possible to define a class named var or dynamic?

查看:104
本文介绍了为什么可以定义一个名为var或dynamic的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我注意到我们可以定义一个名为 var dynamic 的类。首先,我认为这是不可能的,因为它们是特殊的关键字,不能用作类名。,但是后来我尝试了一下,感到很惊讶。当我定义两个类时,这是一个奇怪的情况:

  class var 
{

}

类动态
{

}

并像这样使用它们:





它不会给我任何错误。但是当我尝试运行程序时,它会显示以下错误:


无法将类型字符串隐式转换为动态


但是注意到,我的 dynamic 类的行为类似于标准的C#类。当我尝试这样做时:

  dynamic d = new dynamic(); 

我收到此错误:


无法实例化动态对象。


让我们看看 var:





在这种情况下,var仍被评估为隐式类型定义关键字,而我的变量类型则推断为字符串(注意,它的颜色是绿色而不是蓝色,这是类名color)。我运行的程序给了我同样的例外:


无法将类型字符串隐式转换为var


但是有趣的是,当我尝试创建这样的新var实例时:

  var d = new var(); 
Console.WriteLine(d.GetType());

它没有给我任何例外,我得到了以下输出:

  ConsoleApplication3.var 

现在毕竟,我很困惑。我想知道这里有什么区别,为什么编译器让我们创建名为 var dynamic ,当我创建一个名为dynamic的类时,它不会给我任何错误,但是如果我尝试创建dynamic的实例,我会得到一个错误。但是如果我尝试,它不会给我任何错误创建一个 var 实例。这不是很令人困惑吗?我正在等待明确的解释。



注意:很抱歉我的英语和语法错误。

解决方案

这些被称为 上下文关键字

只有当您自己的类型没有这些名称时,它们才会充当关键字。





由于这些关键字是由较新版本的C#(3.0和4.0)引入的,因此(尽管不太可能)一些现有的代码库具有带有这些名称的类,就像您制作的一样。



因此,如果编译器看到了此类,它将完全按照以前的方式进行编译并将其视为常规类而不是关键字。


Today I noticed we can define a class named var or dynamic. First, I thought it's not possible because these are special keywords and cannot be used as a Class name.But then I tried it,and I'm surprised. Here is a strange situation,when I define two class:

class var
{

}

class dynamic
{

}

And use them like this:

It doesn't give me any error.But when I try to Run my program it gives this error:

Cannot implicitly convert type string to dynamic

But As you may have noticed, my dynamic class behave like a standart C# class.When I try this:

 dynamic d = new dynamic();

I got this error:

Cannot instantiate dynamic object.

Let's look at var:

In this case var is still evaluated as implicit type definition keyword and my variable type infer as string.(notice that it's color is green not blue which is class name color).But when I Run the program it gave me the same exception:

Cannot implicitly convert type string to var

But interestingly when I try to create a new var instance like this:

var d = new var();
Console.WriteLine(d.GetType());

It doesn't give me any exception and I get this output:

ConsoleApplication3.var

Now after all of these I'm confused.I'm wondering what is the difference here and why compiler let us to create classes named var and dynamic,It doesn't give me any error when I creating a class named dynamic but if I try to create instance of dynamic I'm getting an error.But it doesn't give me any error if I try to create a var instance.Isn't that confusing ? I'm waiting for clear explanation.

Note: Sorry for my bad english and grammar mistakes.

解决方案

These are called contextual keywords.
They only behave as keywords if you don't have your own types with those names.

The point of this is to allow exactly what you just did.

Because these keywords were introduced by newer versions of C# (3.0 and 4.0), it's possible (though unlikely) that some existing codebases have classes with those names, just like you made.

Therefore, if the compiler sees such classes, it will compile exactly the way it used to and treat them as regular classes rather than keywords.

这篇关于为什么可以定义一个名为var或dynamic的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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