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?
nameof(Object)
instead.nameof()
doesn't work on synonyms, only on the original classnames. – NickeyObject
's name as string – Nickeynameof(int)
vsnameof(Int32)
– NickeyObject
andobject
are entirely interchangeable. See here. This question shows that in fact, they are not the same. – Siliciousobject
is a synonym forObject
andnameof()
doesn't work on synonyms. – Nickeyobject
is a keyword in the C# language. Keywords are rather a big deal to compilers, they disambiguate syntax and help generate good error messages. But with the hang-up that keywords are not identifiers so nameof() can't work. – Suprematismobject
in the specific case of parsing the nameof operand. It would however greatly uglify the expression parser. – Suprematism)
, expecting.
" afternameof(object)
? :) – Tildennameof()
statement would have slowed down the parsing of the code? I'm not expert of low-level stuffs like compiler parsing, so it's a honest question. – Dominyobject
,string
,int
, etc. But only in the specific case of it being used to parse the nameof operand. And still deal with the wonky cases likeobject int
and produce a decent error message for them. The expression parser is one of the most difficult parts of a compiler, there is lots of expressive power in a language like C#. The less special cases, the lower the risk that it accidentally accepts malformed code or spits out a bad diagnostic. – Suprematism