typeof keyword

  • Thread starter Thread starter ichor
  • Start date Start date
I

ichor

hi
what is the use of the typeof keyword , and how does it differ from the
GetType method. i found the GetType method useful but i fail to understand
the use of the typeof method.

i have tried out a very simple example which i made up myself.

Employee e = new Employee();
ContractEmp ec =new ContractEmp();
MessageBox.Show(typeof(Employee).ToString());
Type T = e.GetType ();
MessageBox.Show (T.ToString());


any other examples which outline their specific uses would be appreciated.

also the typeof keyword always fails in the immidiate window. why is that .
the GetType method works fine.
 
hi
what is the use of the typeof keyword , and how does it differ from the
GetType method. i found the GetType method useful but i fail to
understand the use of the typeof method.

i have tried out a very simple example which i made up myself.

Employee e = new Employee();
ContractEmp ec =new ContractEmp();
MessageBox.Show(typeof(Employee).ToString());
Type T = e.GetType ();
MessageBox.Show (T.ToString());

any other examples which outline their specific uses would be
appreciated.

// GetType requires instance:
Employee e = new Employee();
Type t = e.GetType();

// typeof doesn't
Type t = typeof(Employee);

// you can also use the static method GetType of Type:
Type t = Type.GetType("FullNameSpace.Employee");
also the typeof keyword always fails in the immidiate window. why is
that . the GetType method works fine.

Because the immediate window is an access window to instances or
methods available in the current scope. typeof() is a statement which
needs compilation into IL to be effective. Use Type.GetType()

FB
 
ichor said:
what is the use of the typeof keyword , and how does it differ from the
GetType method. i found the GetType method useful but i fail to understand
the use of the typeof method.

i have tried out a very simple example which i made up myself.

Employee e = new Employee();
ContractEmp ec =new ContractEmp();
MessageBox.Show(typeof(Employee).ToString());
Type T = e.GetType ();
MessageBox.Show (T.ToString());

Doesn't that already show the difference? I think it does, the typeof
operator is applied to a type name (e.g. the class Employee) while
GetType is a method that you call on an instance of a type (e.g. the e
variable) on run-time.
 
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
fbhca
 
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 said:
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.
 
Back
Top