473,473 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Differences between typeof and GetType

Hi all,
Please give me the differences between typeof and GetType

bye

Nov 16 '05 #1
3 22096
srkvellanki,

GetType is a call that is made at runtime on an instance of an object.
typeof() is resolved to a call at runtime, but loads the type from the token
for the type. They probably resolve to the same call, it's just that
GetType requires an instance. If I didn't need the instance, I would use
typeof.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"srkvellanki" <sr*********@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
Hi all,
Please give me the differences between typeof and GetType

bye

Nov 16 '05 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u5**************@TK2MSFTNGP09.phx.gbl...
srkvellanki,

GetType is a call that is made at runtime on an instance of an object.
typeof() is resolved to a call at runtime, but loads the type from the
token for the type. They probably resolve to the same call, it's just
that GetType requires an instance. If I didn't need the instance, I would
use typeof.

Hope this helps.


Also be aware that GetType is virtual, and gives you the type of the object,
not the declared type of the reference. That is:

Object o = new String();

typeof(Object) Object type
typeof(o) String type
Nov 16 '05 #3
Hi, srkvellanki.
I assume by GetType you mean Type.GetType. The difference between
Object.GetType and typeof has been explained in previous two posts.
typeof is resolved at compile time, so the type has to be either in
current assembly or one of the assemblies it reference to. Type.GetType on
the other hand, resolves the full qualified type name (namespace + assembly
information) at runtime, so it can be used to load types defined in any
assembly that can be located at runtime. Another note about Type.GetType is,
when called with a typename without the defining assembly, it only looks for
the type in calling assembly and mscorlib.dll.

Hope this helps.
Ming Chen
"srkvellanki" <sr*********@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
Hi all,
Please give me the differences between typeof and GetType

bye

Nov 16 '05 #4

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

Similar topics

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...
0
by: srkvellanki | last post by:
Hi All, I want to know the differences between typeof and GetType with regards
4
by: Peter Kirk | last post by:
Hi is there a difference between "this.GetType()" and typeof(MyClass)? The only reason I ask is that we are using log4net in a project, and we give the Loggers names based on the class name....
10
by: Jim Heavey | last post by:
I want to know if the type of data of one object is the same type as another object. The "TypeOf" function let's me find out it the object is integer or string or.... but it does not seem to allow...
1
by: Brien King | last post by:
Ok, I have three classes (The example here is extremely simplified to illustrate the problem) like this: Public Class A Public Sub DoSomething(ByVal myClass) If TypeOf myClass IS A Then ' '...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
6
by: lothar.behrens | last post by:
Hi, I have some code, where I am using a variable for a given interface. One step later, using Remoting, I need Type.TypeOf("...") The following code returns Nothing, but the type above could...
2
by: Muckeypuck | last post by:
hello, i would like to write a function that takes a webcontrol type as a parameter and returns an array of controls based on the type some thing like: ...
0
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...
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,...
1
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...
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.