Why is nameof(object) not allowed?

无人久伴 提交于 2019-12-19 17:42:38

问题


In C# 6.0 you can write this:

var instance = default(object);
var type = typeof(object);

They have the same result of:

var instance = default(System.Object);
var type = typeof(System.Object);

But you can't write this:

var name = nameof(object);

It generates the following error:

Invalid expression term 'object'.

But you can still write this:

var name = nameof(System.Object);

Why nameof(object) does not compile?


回答1:


The difference is that object is a synonym for the class Object and nameof() doesn't work on synonyms.

Same applies to nameof(int) vs nameof(Int32)



来源:https://stackoverflow.com/questions/40421961/why-is-nameofobject-not-allowed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!