How to Get Type Name without full namespace in C# ?

This post will explain in simple steps on how to get type name without full name in C# using the typeof operator.

If you want to find the full name of the type in C# , you can use the typeof keyword to do it as shown in the below code snippet.

 var str1 = typeof (Author).ToString();

The problem with this method is that it displays the full name along with the namespace.

If you want to get only the class name without namespace , you can use the Name property of MemberInfo class as shown below.

var str2 = typeof(Author).Name;
Console.WriteLine("using typeof(Author).Name results in " + str2);

If you are working on C# 6 , you can use the nameof operator to achieve the same as shown below.

var str3 = nameof(Author);
Console.WriteLine("using nameof(Author) results in " + str3);

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...