Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it always the case that the nameof() is equal to the typeof().Name?

Tags:

c#

reflection

I've tested Class, Methods, Fields, Properties, and Enums to see if there are any cases when this is not true?

DotNetFiddle Example

using System;

public class Program
{
    public static void Main()
    {
        var fooType = typeof(Foo);
        ThrowIfNotEqual(fooType.Name, nameof(Foo));

        var fi = fooType.GetField(nameof(Foo.field));
        ThrowIfNotEqual(fi.Name, nameof(Foo.field));

        var pi = fooType.GetProperty(nameof(Foo.property));
        ThrowIfNotEqual(pi.Name, nameof(Foo.property));

        var mi = fooType.GetMethod(nameof(Foo.method));
        ThrowIfNotEqual(mi.Name, nameof(Foo.method));

        var fi2 = fooType.GetNestedTypes()[0];
        ThrowIfNotEqual(fi2.Name, nameof(Foo.myEnum));

        ThrowIfNotEqual("TestThisMethod", "WorksAsExpected");
    }

    public static void ThrowIfNotEqual(string a, string b)
    {
        if (a != b) throw new InvalidOperationException($"Are Not Equal: {a} != {b}");
    }

    public class Foo
    {
        public string field;
        public string property { get; set; }
        public void method() { }
        public enum myEnum
        {
            A
        }
    }
}

Results:

Run-time exception (line -1): Are Not Equal: TestThisMethod != WorksAsExpected

like image 679
Erik Philips Avatar asked Jan 18 '19 01:01

Erik Philips


People also ask

What is the difference between Nameof and Typeof?

Typeof returns Type objects. It is often used as a parameter or as a variable or field. The C# typeof operator is part of an expression that acquires the Type pointer. Nameof, meanwhile, returns a string with a variable's name.

What is Nameof method?

A nameof expression produces the name of a variable, type, or member as the string constant: C# Copy.

Why do we use Nameof in C#?

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. We can use it for logging, validating parameters, checking events etc.

When was Nameof added to C#?

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) .


1 Answers

Is it always the case that the nameof() is equal to the typeof().Name?

No there are lots ways to break this, just as an example

public class Foo<T>

E.g

var fooType = typeof(Foo<string>);
Console.WriteLine(fooType.Name);
Console.WriteLine(nameof(Foo<string>));

Output

Foo`1
Foo

There are also many situations where you will get compiler errors on just predefined types using nameof()

Console.WriteLine(nameof(int)); //CS1525    Invalid expression term 'int'   

nameof (C# Reference)

Remarks

Because the argument needs to be an expression syntactically, there are many things disallowed that are not useful to list. The following are worth mentioning that produce errors

like image 186
TheGeneral Avatar answered Oct 06 '22 00:10

TheGeneral