Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nameof won´t reflect using

Tags:

c#

using

nameof

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?

like image 383
MakePeaceGreatAgain Avatar asked Oct 15 '22 22:10

MakePeaceGreatAgain


1 Answers

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

like image 94
LoRdPMN Avatar answered Oct 18 '22 19:10

LoRdPMN