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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...