From this question I learned that the nameof() operator, introduced in C# 6.0, does not work on synonymous. So you can write nameof(System.Object) but not nameof(object).
Now, there are 2 other similar operators, typeof() and default(), and they work perfectly on synonymous. You can write:
default(object);
default(int);
typeof(string);
typeof(long);
as well as:
default(Object);
default(Int32);
typeof(String);
typeof(Int64);
and the results are the same.
I guess that it would have been possible to build the nameof() operator to operate with synonymous too.
So the question is: WHY it was not implemented to work with object, string, int, etc.? Is my guess wrong (i.e. it's impossible to implement)? Or was it a design choice?
I think it's a design choice.
typeof()operator returns aType. SinceobjectandSystem.Objectexpress the same exactType, there is no problem in writingtypeof(object).The same applies to
default(): it isnullin both cases (or0or whatever).Now, for
nameof(), the context is a bit harder.Pretend that
nameof(object)is allowed. What should it return?"object". But now the result is different fromnameof(System.Object), and it makes no sense, given that the class is the same."Object"uppercase, the same asnameof(System.Object). But now we have a strange exception on the rule:nameof()returns the exact string representation of the expression passed as parameter... exceptobject(and the other synonymous). This can cause subtle issues, for example I might give for sure thatnameof(object)returns"object"sincenameof(Table)returns"Table"andnameof(MyComponent.SomeProperty)returns"SomeProperty", and so on.Making
nameof()not available on synonymous definitely resolved the ambiguity.