New! GraphQL.NET Cookbook by Senthil Kumar Balu

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

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

image

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);
Senthil Kumar B
Senthil Kumar is a former Microsoft MVP (Most Valuable Professional). He is a Co-Author of the book "Windows 10 Development Recipes using JavaScript and CSS" for Apress Publication. He is a technical presenter, blogger, mentor and a Geek.  Senthil is a regular speaker is various local user groups. He has presented at conferences like Great Indian Developer Summit (GIDS) & Microsoft DevCamps. You can reach out to him via his Twitter handle @isenthil.