What's the typeof() function for?

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

Would someone give me an example for the typeof() function? What's that
for?
Thanks for help.

Jason
 
Jason Huang said:
Hi,

Would someone give me an example for the typeof() function? What's that
for?
Thanks for help.

Jason

Hi

if u need to use serilize the object u have to specify the type of that
object
that situation we need to use (typeof("Class Name"), Class object)

Regards
sambath R
 
Hi Jason,
typeof returns a Type instance that contains all of the type information
for a particular type, this is used for reflection where you want to look
inside a type to see maybe how many methods it has, the return type of a
method etc all dynamically. For example you could use the type information
to get all of the static methods of the string class like:


using System;
//using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
Type t = typeof(string);

MethodInfo[] methods = t.GetMethods();

foreach (MethodInfo method in methods)
{
if(method.IsStatic)
{
Console.WriteLine(method.Name);
}
}

Console.ReadLine();
}
}
}


This is a really quick and brief explanation, start researching more into
reflection to see the power of this information.

Hope that helps
Mark R Dawson
http://www.markdawson.org
 
Jason,

If you run through a collection with more types of objects and you want to
handle those than if they are not all derived from the same parent and you
only needs the derived methods/properties, than you need to know what type
of object you are handling.

Where in my opinion the collection of controls in a control is the nicest
sample, if you want to change all checkboxes than you need to know that the
type of the control is a checkbox.

I hope this helps,

Cor
 
// static stuff
private static int uniqueID= 0;
private static int GetUniqueID()
{
lock(typeof(TestStatic))
{
return uniqueID++; // returns zero at start
}
}

Regards,
Jeff
 
Cor Ligthert said:
If you run through a collection with more types of objects and you want to
handle those than if they are not all derived from the same parent and you
only needs the derived methods/properties, than you need to know what type
of object you are handling.

Where in my opinion the collection of controls in a control is the nicest
sample, if you want to change all checkboxes than you need to know that the
type of the control is a checkbox.

But in that case, the best thing to use is "as" or "is":

CheckBox cb = foo as CheckBox;
if (cb != null)
{
...
}

or

if (foo is CheckBox)

typeof(...) is used when you need an expression which evaluates to
Type, eg for ArrayList.ToArray.
 
Jon,

Interesting, will you be so kind to explain this to me more, what is the
equivalent of this in C# (seriously not a challenge).

\\\
Private Sub SetAllCheckStateTrue(ByVal parentCtr As Control)
For Each ctr As Control In parentCtr.Controls
If TypeOf ctr Is CheckBox Then
DirectCast(ctr, CheckBox).CheckState = CheckState.Checked
SetAllCheckStateTrue(ctr)
End If
Next
End Sub
///

Thanks in advance.

Cor
 
Jon,

Not needed

private void SetAllCheckStateTrue(Control parentCtr)
{
foreach (Control ctr in parentCtr.Controls)
{
if (ctr is CheckBox)
{
((CheckBox)ctr).CheckState = CheckState.Checked;
SetAllCheckStateTrue(ctr);
}
}
}

:-)

Cor
 
Cor Ligthert said:
Interesting, will you be so kind to explain this to me more, what is the
equivalent of this in C# (seriously not a challenge).

\\\
Private Sub SetAllCheckStateTrue(ByVal parentCtr As Control)
For Each ctr As Control In parentCtr.Controls
If TypeOf ctr Is CheckBox Then
DirectCast(ctr, CheckBox).CheckState = CheckState.Checked
SetAllCheckStateTrue(ctr)
End If
Next
End Sub
///

Well, the equivalent but without the extra cast cost is:

private SetAllCheckStateTrue (Control parentCtr)
{
foreach (Control ctr in parentCtr.Controls)
{
CheckBox cb = ctr as CheckBox;
if (cb != null)
{
cb.CheckState = CheckState.Checked;
SetAllCheckStateTrue(ctr);
}
}
}

I *suspect* you meant to put the recursive call outside the "if"
though.
 
Cor Ligthert said:
Not needed

private void SetAllCheckStateTrue(Control parentCtr)
{
foreach (Control ctr in parentCtr.Controls)
{
if (ctr is CheckBox)
{
((CheckBox)ctr).CheckState = CheckState.Checked;
SetAllCheckStateTrue(ctr);
}
}
}

While that works, there's no need to effectively cast twice - once in
the "is" and once on the following line. Using "as" and then checking
for null is a bit more efficient and just as readable, so for patterns
like the above it's better. (I use direct casting when it's an error
for the reference to be anything other than the target type - I like an
appropriate exception to be generated immediately.)
 
Hi,

To get a reference to a Type using a class instead of an instance.

If you have an instance you use GetType() which return an instance of the
Type class. If cases that you do not have an instance you get a similar
result using typeof( Type).
Why you need for or how you use a Type is another matter, check MSDN or just
post back for further info.

by the way, typeof is an operator, not a function, remember that C# has no
"global" function any function( or method) should be declared inside a
class.


cheers,
 
Back
Top