Why is nameof(object) not allowed?

2019-02-21 22:10发布

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?

标签: c# c#-6.0 nameof
1条回答
唯我独甜
2楼-- · 2019-02-21 23:02

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)

查看更多
登录 后发表回答