473,850 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ 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(Employe e).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 4816
"ichor" <ch***@hotmail. com> wrote in
news:OY******** ******@tk2msftn gp13.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(Employe e).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("F ullNameSpace.Em ployee");
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(Employe e).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.WriteLi ne(o.GetType(). ToString());//Would print something like:
"ConsoleApplica tion1.Employee"
//However, after exection of the following statement
o=System.DateTi me.Now;
//The same o.GetType().ToS tring() would print "System.DateTim e" for the same
"o" object
Console.WriteLi ne(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*******@disc ussions.microso ft.com> wrote in message
news:AF******** *************** ***********@mic rosoft.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
42578
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 = window.open('http://www.yahoo.com/'); alert(typeof x); And I found this page: http://www.mozilla.org/js/language/js20-2002-04/core/expressions.html
4
3045
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 how to reduce the if statement? thanks
4
536
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 out a very simple example which i made up myself. Employee e = new Employee(); ContractEmp ec =new ContractEmp(); MessageBox.Show(typeof(Employee).ToString());
7
9986
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 method. The method could look like this: public void myDynamicMethod(Type type){ //.....do something with the Type passed in....... }
6
7235
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
57181
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
5628
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
20
9326
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues = displayValuesArray.length; for (i=0; i<nDisplayValues; i++) {
20
21798
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() macro, but the same code is not getting compiled in solaris forte compiler and in microsoft VS 2003 compiler. I tried something like below:
6
2215
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 is can i make this dynamic. I throws an error on run that it can't find the namespace. I understand why, but is what i'm trying to do possible? public static void BindEnumToDropDown(ref DropDownList ddl, Enum enm) { FieldInfo myEnumFields =...
0
9895
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9741
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10666
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10724
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10351
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7071
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4546
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 we have to send another system
3
3178
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.