Code highlighted is doing two jobs first it gets type then converts that type into lower case letter. How can it be split into two codes, that mean 1st code gets type and 2nd code converts that type into lower case letter.
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 type = p.GetType().Name.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

Shivanand ArurRank #920 · 2.3K reputationPosted Sep 22, 2012, 10:12 PM EDT
string type = p.GetType().Name;
char initial = p.Name[0];
Console.WriteLine("{0} is a {1} and his initial is {2}", p.Name, type.ToLower(), initial);
Hope this is what you wanted...
Posted Sep 23, 2012, 12:33 AM EDT
variable_type nameOFclass = p.GetType();
string type = nameOFclass.ToLower();
Shivanand ArurRank #920 · 2.3K reputationPosted Sep 23, 2012, 1:14 AM EDT
Posted Sep 23, 2012, 4:42 AM EDT
string nameOFclass = (string)p.GetType();
string type = nameOFclass.ToLower();
VulpesRank #55 · 29.2K reputationPosted Sep 23, 2012, 5:45 AM EDT
Consequently, you either have to use the Name property, the FullName property or apply the ToString() method which is overridden to provide the fully qualified name of the type as well:
Posted Sep 23, 2012, 5:52 AM EDT
Posted Sep 23, 2012, 6:08 AM EDT
VulpesRank #55 · 29.2K reputationPosted Sep 23, 2012, 6:30 AM EDT
public override string ToString()
ToString() is, of course, a virtual method in the System.Object class which all types inherit and which they can override if they like. If they don't override it, then it returns the fully qualified name of the type of object to which it's applied.
So, if the Type class hadn't been overridden, then p.GetType().ToString() would have returned 'System.Type' because it's applied to a Type object.
However, it has been overridden to provide instead the fully qualified name of the type represented by the Type object. If 'p' refers to a President object, it will therefore return 'President'.
Posted Sep 23, 2012, 6:37 AM EDT
What is different between a virtual method and method.
VulpesRank #55 · 29.2K reputationPosted Sep 23, 2012, 6:48 AM EDT
Alternatively, press WJ whilst holding down the Control key.
You can then navigate to any type you like. The Type class is in mscorlib under the System namespace.
A virtual method differs from an ordinary method in that it is polymorphic and can be overridden in derived classes. The version of the virtual method which gets called at runtime is then determined by the runtime type of the object to which it's applied, not its compile time type.
Posted Sep 23, 2012, 11:07 AM EDT
Posted Sep 23, 2012, 4:54 PM EDT
public override string ToString()
How can p.GetType().ToString() be similar to the above one. Because syntax of this is quite different to the above one.
VulpesRank #55 · 29.2K reputationPosted Sep 24, 2012, 4:26 AM EDT
You don't use it in expressions such as p.GetType().ToString() though the value of this expression will depend on whether ToString() is overridden or not - it is, of course, in this particular case.
Posted Sep 24, 2012, 4:35 AM EDT