nedcomp hosting homepage

Producten en Diensten
Dedicated servers
Datacenter informatie
Partners, resellers
Helpdesk informatie
Technische docs, tools
Support homepage
ASP componenten
Praktische ASP, ASP.NET
Visual route server
Whois (domein gegevens)
Software documentatie
Whitepapers
Zoeken
Nedcomp / algemeen

Zoeken
 

Copyright © Nedcomp Hosting
Telefoon nr :   +31 184 670111
Fax nummer :   +31 184 631384
E-mailadres :   info@nedcomp.nl
 

C# Programmer's Reference  

typeof

The typeof operator is used to obtain the System.Type object for a type. A typeof expression takes the form:

typeof(type)

where:

type
The type for which the System.Type object is obtained.

Remarks

The typeof operator cannot be overloaded.

To obtain the run-time type of an expression, you can use the .NET Framework method GetType.

Example

// cs_operator_typeof.cs
// Using typeof operator
using System;
using System.Reflection;

public class MyClass 
{
   public int intI;
   public void MyMeth() 
   {
   }

   public static void Main() 
   {
      Type t = typeof(MyClass);

      // alternatively, you could use
      // MyClass t1 = new MyClass();
      // Type t = t1.GetType();

      MethodInfo[] x = t.GetMethods();
      foreach (MethodInfo xtemp in x) 
      {
         Console.WriteLine(xtemp.ToString());
      }

      Console.WriteLine();

      MemberInfo[] x2 = t.GetMembers();
      foreach (MemberInfo xtemp2 in x2) 
      {
         Console.WriteLine(xtemp2.ToString());
      }
   }
}

Output

Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void MyMeth()
Void Main()
System.Type GetType()

Int32 intI
Int32 GetHashCode()
Boolean Equals(System.Object)
System.String ToString()
Void MyMeth()
Void Main()
System.Type GetType()
Void .ctor()

Example

// cs_operator_typeof2.cs
// Using GetType method
using System;
class GetTypeTest 
{
   public static void Main() 
   {
      int radius = 3;
      Console.WriteLine("Area = {0}", radius*radius*Math.PI);
      Console.WriteLine("The type is {0}", 
               (radius*radius*Math.PI).GetType());
   }
}

Output

Area = 28.2743338823081
The type is System.Double

See Also

C# Keywords | is | Operator Keywords