http://www.c-sharpcorner.com/Forums/Thread/153399/
This example is given in the above website. If ToString() method is replaced by "Name" the output is same. Please explain the reason. Problem is highlighted.
string nameOFclass = p.GetType().Name;
using System;
interface person
{
String Name { get; set; }
}
class Poster : person
{
public String Name { get; set; }
}
class President : person
{
public String Name { get; set; }
public int Age { get; set; }
}
class Test
{
static void Main()
{
Poster po = new Poster();
po.Name = "Michell";
PrintDetails(po);
President pr = new President();
pr.Name = "Barack";
pr.Age = 50;
PrintDetails(pr);
Console.ReadKey();
}
static void PrintDetails(person p)
{
string nameOFclass = p.GetType().ToString();
string type = nameOFclass.ToLower();
char initial = p.Name[0];
Console.WriteLine("{0} is a {1} and his initial is {2}", p.Name, type, initial);
}
}
/*
Michell is a poster and his initial is M
Barack is a president and his initial is B
*/
Loading
Sanjeeb LenkaPosted Sep 24, 2013, 12:46 AM
p.GetType().ToString();//get class name and name space and convert it to string
p.GetType().Name // class name, no namespace
p.GetType().FullName // namespace and class name
p.GetType().Namespace // namespace, no class name
Posted Sep 24, 2013, 6:24 AM
Jignesh TrivediPosted Sep 24, 2013, 1:10 AM
hi,
GetType method return type of current instance.
Type is support many property and methods,
Please refer for more information
http://msdn.microsoft.com/en-us/library/system.type.aspx
hope this will help you.