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# NameOf operator is used to get name of a variable, class or method. It returns a simple string as a result. In error prone code, it is useful to capture a method name, in which error occurred.
The nameof operator, added in C# 6.0, addresses this — it allows capturing the string names of symbols that are in the scope. In the example below, ReSharper suggests the replacement of the string literal "order" in the argument of the ArgumentNullException() with the nameof(order) .
idiom. : used to indicate the name that is used for someone or something.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With