I have an alias within my source-code file like this:
MyClass.cs
using System;
using SomeClass = NewClass;
public class Program
{
public static void Main()
{
Console.WriteLine(nameof(SomeClass));
}
}
public class NewClass { }
I need this because the names of my classes changed and now I need to compile for both the old and the new class-structure symultaneously.
When I run that code I get "SomeClass"
but I' d expected "NewClass"
. Why doesn't nameof
reflect the alias using the using
-directive in this case?
It's because the nameof
keyword is meant to "get the name of an identifier", without evaluating its value or anything else, like said in docs:
A nameof expression is evaluated at compile time and has no effect at run time.
More info here
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