Home

Built-In Classes: Type

     

Introduction

In some cases, when you have a variable, you may need to identify the class name of that variable. To assist you with this, the .NET Framework provides an abstract class named Type. The Type class is created in the System namespace.

 

Because Type is an abstract class, you cannot declare a variable of it. To assist you with finding out the data type or the class of a variable, the C# language provides an operator named typeof. Its formula is:

Type type = typeof(DataType | ClassName);

typeof is a unary operator that acts on one operand. The operand can be a known primitive data type or a class. If you pass a C# data type (such as char, int, float, etc) as the operand, this operator produces the equivalent .NET Framework type. In reality, the typeof operator produces the Type name of its operand. Here is an example of using it:

using System;

public class Exercise
{
    public static int Main()
    {
        Type tp = typeof(int);

        Console.WriteLine("The type of the int is {0}", tp);

        return 0;
    }
}

This would produce:

The type of the int is System.Int32
Press any key to continue . . .

You will usually need the services of the Type class to know the data type of a variable or of an argument to a method. To make this possible, the Object class is equipped with a method named GetType. Its syntax is:

public Type GetType();

This method takes no argument and returns the name of the class or the data type of the variable that called it. If the variable that calls this function is of a primitive C# data type, this method returns its .NET Framework equivalent. Here is an example of calling this method:

using System;

public class Exercise
{
    public static int Main()
    {
        var number = 22705;
        Type tp = number.GetType();

        Console.WriteLine("The number variable is of type {0}", tp);

        return 0;
    }
}

This would produce:

The number variable is of type System.Int32
Press any key to continue . . .

The Type.GetType() method gives you the name of the class of a variable. If the class is derived from another class and you want to know the name of that parent class, the Type class can help you, using a property named BaseType. Here is an example of using it:

using System;

public abstract class RoundShape
{
    public virtual double Radius { get; set; }
    public abstract double Diameter { get; }
    public abstract double Area();
}

public class Circle : RoundShape
{
    private double rad;

    public sealed override double Radius
    {
        get
        {
            return this.rad;
        }

        set
        {
            this.rad = value;
        }
    }

    public override sealed double Diameter
    {
        get
        {
            return this.rad * 2;
        }
    }

    // Don't mark this method as "sealed" because the sphere must override it
    public override double Area()
    {
        return this.rad * this.rad * 3.14159;
    }
}

public sealed class Sphere : Circle
{
    // We can now seal this method because we don't intend
    // to override it down. Its class is sealed already anyway
    public override sealed double Area()
    {
        return 4 * this.Radius * this.Radius * 3.14159;
    }

    // Cannot use "sealed" because this method is not inherited
    public double Volume()
    {
        return 4 * this.Radius * this.Radius * this.Radius * 3.14159 / 4;
    }
}

public class Exercise
{
    private static int Main(string[] args)
    {
        Circle round = new Circle();
        Type plate = round.GetType();

        Console.Write("The parent class of the type of ");
        Console.WriteLine("the round variable is {0}", plate.BaseType);

        Sphere planete = new Sphere();
        Type plane = planete.GetType();

        Console.Write("The parent class of the type of ");
        Console.WriteLine("the planete variable is {0}", plane.BaseType);

        return 0;
    }
}

This would produce:

The parent class of the type of the round variable is RoundShape
The parent class of the type of the planete variable is Circle
Press any key to continue . . .

In the same way, you can use a type of recursion which would consist of accessing the BaseType of the BaseType of the BaseType ... of a variable. Here is an example:

public class Exercise
{
    private static int Main(string[] args)
    {
        Circle round = new Circle();
        Type plate = round.GetType();

        Console.Write("The parent class of the type of ");
        Console.WriteLine("the round variable is {0}", plate.BaseType);

        Sphere planete = new Sphere();
        Type plane = planete.GetType();

        Console.Write("The parent class of the type of ");
        Console.WriteLine("the planete variable is {0}", plane.BaseType);

        Console.Write("The parent class of the type of ");
        Console.WriteLine("the round variable is {0}", plate.BaseType.BaseType);

        return 0;
    }
}

This would produce:

The parent class of the type of the round variable is RoundShape
The parent class of the type of the planete variable is Circle
The parent class of the type of the round variable is System.Object
Press any key to continue . . .

Sometimes you need more information. For example if a class is defined in an assembly, you may want to know what that assembly is. To assist you with getting this information, the Type class is equipped with a property named Assembly. Here is an example of finding out the assembly in which the type of a variable in defined:

using System;

public class Exercise
{
    public static int Main()
    {
        var number = 22705;
        Type tp = number.GetType();

        Console.Write("The type of the number variable is {0}, ", tp);
        Console.WriteLine("which is defined in the {0} assembly.", tp.Assembly);
        
        return 0;
    }
}

This would produce:

The type of the number variable is System.Int32, which is defined in the mscorlib,
 Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 assembly.
Press any key to continue . . .

When accessing the Type.Assembly property, make sure the class of the variable you are using has a formal assembly. Otherwise you would receive an error.

To get the complete name of the class of a variable without the assembly, use the Type.FullName property.

Besides the assembly in which the class of a variable exists, you may want to know the namespace in which that class is defined. To get this information, the Type class is equipped with the Namespace property. Here is an example of accessing it:

using System;
using Geometry;
using Volumes;

namespace Geometry
{
    public class Square
    {
    }

    public class Triangle
    {
    }
}

namespace Volumes
{
    public class Cube : Geometry.Square
    {
    }
}

public class Exercise
{
    private static int Main(string[] args)
    {
        Square sqr = new Square();
        Cube cb = new Cube();

        Type napkin = sqr.GetType();
        Type box = cb.GetType();

        Console.Write("The namespace of the class of the sqr variable is ");
        Console.WriteLine(napkin.Namespace);
        Console.Write("The namespace of the class of the cb variable is: ");
        Console.WriteLine(box.Namespace);

        return 0;
    }
}

This would produce:

The namespace of the class of the sqr variable is Geometry
The namespace of the class of the cb variable is: Volumes
Press any key to continue . . .

Besides the name of the class of a variable, another valuable piece of information to find out about a class is its classification. That is, sometimes when using a type, you may want to know whether it is a simple regular class, an abstract class, an interface, an enumeration, or another type we haven't studied yet (structure, generic, etc). To assist you with checking this, the Type class provides various Boolean properties such as IsAbstract or IsClass. Here is an example of using them:

using System;

public abstract class RoundShape
{
    public virtual double Radius { get; set; }
    public abstract double Diameter { get; }
    public abstract double Area();
}

public class Circle : RoundShape
{
    private double rad;

    public sealed override double Radius
    {
        get
        {
            return this.rad;
        }

        set
        {
            this.rad = value;
        }
    }

    public override sealed double Diameter
    {
        get
        {
            return this.rad * 2;
        }
    }

    // Don't mark this method as "sealed" because the sphere must override it
    public override double Area()
    {
        return this.rad * this.rad * 3.14159;
    }
}

public class Exercise
{
    private static int Main(string[] args)
    {
        Circle round = new Circle();
        Type plate = round.GetType();

        Console.WriteLine("The type of the round variable is:");
        Console.WriteLine("Abstract: {0}", plate.IsAbstract);
        Console.WriteLine("A Class:  {0}", plate.IsClass);

        return 0;
    }
}

This would produce:

The type of the round variable is:
Abstract: False
A Class:  True
Press any key to continue . . .
 
 

Home Copyright © 2010-2016, FunctionX