473,472 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

typeof keyword

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.
Nov 16 '05 #1
4 4795
"ichor" <ch***@hotmail.com> wrote in
news:OY**************@tk2msftngp13.phx.gbl:
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

--
Get LLBLGen Pro, the new O/R mapper for .NET: http://www.llblgen.com
My .NET Blog: http://weblogs.asp.net/fbouma
Microsoft C# MVP
Nov 16 '05 #2


ichor wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #3
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

Nov 16 '05 #4
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

Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Bennett Haselton | last post by:
Is there any way to find a string representing an object's class, which will work in Internet Explorer 6? "typeof" doesn't work -- it returns "object" for all objects: x =...
4
by: rua17 | last post by:
Hello: I want to know if somo variable is integer, real or string but typeof returns Int16, Int32, etc, because of that my if() statement has to compare if X is Int32 or Int64 or ... Any idea...
4
by: ichor | last post by:
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...
7
by: Mark Miller | last post by:
I am using Reflection.Emit to dynamically build a class. A method of the class to be built requires a Parameter of type "Type". But I don't know how to use Emit to pass a call of "typeof()" to the...
6
by: MM | last post by:
Very basic question: string mystr how do you test if it is a string? Something like if (mystr.GetType() == "System.String") <<< but this doesn't work.
6
by: Michael C# | last post by:
What's the VB equivalent of the C# typeof() operator (it doesn't appear to be the VB "TypeOf" operator). Thanks.
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
20
by: rkk | last post by:
Hi, Is there an equivalent typeof macro/method to determine the type of a variable in runtime & most importantly that works well with most known C compilers? gcc compiler supports typeof()...
6
by: =?Utf-8?B?V2ViQnVpbGRlcjQ1MQ==?= | last post by:
i'm trying to modify some code to accept an enum then use reflection to fill a dropdown list. If i name the type directly, replace enm with the real name of the enum it works, no suprise. Question...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.