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
Posted Sep 24, 2012, 8:35 AM
VulpesPosted Sep 24, 2012, 8:26 AM
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 23, 2012, 8:54 PM
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.
Posted Sep 23, 2012, 3:07 PM
VulpesPosted Sep 23, 2012, 10:48 AM
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, 10:37 AM
What is different between a virtual method and method.
VulpesPosted Sep 23, 2012, 10:30 AM
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, 10:08 AM
Posted Sep 23, 2012, 9:52 AM
VulpesPosted Sep 23, 2012, 9:45 AM
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, 8:42 AM
string nameOFclass = (string)p.GetType();
string type = nameOFclass.ToLower();
Shivanand ArurPosted Sep 23, 2012, 5:14 AM
Posted Sep 23, 2012, 4:33 AM
variable_type nameOFclass = p.GetType();
string type = nameOFclass.ToLower();
Shivanand ArurPosted Sep 23, 2012, 2:12 AM
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...