ichor,
The real value of GetType() method would be when you don't know in advance
the run time type of an object. The typeof() would only give the type of an
already know class, but the GetType would dynamically determine the current
runtime type of any class instance.
Extending the original example:
Object o=new Employee();//load object o with an instance of Emploee class.
Console.WriteLine(o.GetType().ToString());//Would print something like:
"ConsoleApplication1.Employee"
//However, after exection of the following statement
o=System.DateTime.Now;
//The same o.GetType().ToString() would print "System.DateTime" for the same
"o" object
Console.WriteLine(o.GetType().ToString());
I hope this clarifies the reason why would we sometime want to use GetType()
instead of typeof()
Fakher Halim
Software Architect
"fbhcah" <an*******@discussions.microsoft.com> wrote in message
news:AF**********************************@microsof t.com...
GetType is a method, which would require an instance to work. Whereas,
typeof would work with uninitialized entities - types.
So when you don't have an object with you, you will have to use typeof.
This typically happens when you work with static only classes. Also, if you
call GetType upon an uninitialized variable, an exception would result. This
wouldn't happen for typeof, since it uses the type iteself.
HTH,
fbhcah