C# typeof() Operator: Use and Examples

In this tutorial, we will learn about the typeof() operator in C#, its usage with examples. By IncludeHelp Last updated : April 06, 2023

C# typeof() Operator

The typeof() operator is used to get the system type (with the class name) of the given type. By using "typeof() operator, we can get the name of the type, namespace name. It works with only compile-time known types. It does not work with the variables or instances.

If you want to get the type of a variable, you can use GetType() method.

Properties of typeof() Operator

There are main 3 properties to get the details about the type:

  1. typeof(type).Name or this.GetType().Name - It returns the class name only.
  2. typeof(type).FullName or this.GetType().FullName - It returns the class name along with the namespace.
  3. typeof(type).Namespace or this.GetType().Namespace - It returns the namespace only.

Note: If we do not use any property, by default typeof(type) or this.GetType() returns the FullName.

Syntax

System.type typeof(type);
OR    
System.type this.GetType();
typeof(int)     - System.Int32
int a = 10;
a.GetType()     - System.Int32

C# typeof() Operator Example 1

Print the Name, FullName, Namespace name of the compile-time known types

using System;
using System.Text;

namespace Test {
  class Program {
    static void Main(string[] args) {
      Console.WriteLine("for char type...");
      Console.WriteLine("default: " + typeof (char));
      Console.WriteLine("Name: " + typeof (char).Name);
      Console.WriteLine("FullName: " + typeof (char).FullName);
      Console.WriteLine("Namespace: " + typeof (char).Namespace);
      Console.WriteLine();

      Console.WriteLine("for Int32 type...");
      Console.WriteLine("default: " + typeof (Int32));
      Console.WriteLine("Name: " + typeof (Int32).Name);
      Console.WriteLine("FullName: " + typeof (Int32).FullName);
      Console.WriteLine("Namespace: " + typeof (Int32).Namespace);
      Console.WriteLine();

      Console.WriteLine("for bool type...");
      Console.WriteLine("default: " + typeof (bool));
      Console.WriteLine("Name: " + typeof (bool).Name);
      Console.WriteLine("FullName: " + typeof (bool).FullName);
      Console.WriteLine("Namespace: " + typeof (bool).Namespace);
      Console.WriteLine();

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

for char type...
default: System.Char
Name: Char
FullName: System.Char
Namespace: System

for Int32 type...
default: System.Int32
Name: Int32
FullName: System.Int32
Namespace: System

for bool type...
default: System.Boolean
Name: Boolean
FullName: System.Boolean
Namespace: System

C# typeof() Operator Example 2

Print the Name, FullName, Namespace name of the variables.

using System;
using System.Text;

namespace Test {
  class Program {
    //structure
    struct student {
      private string name;
      private int age;
    };
    static void Main(string[] args) {
      char a = 'X';
      int b = 10;
      bool c = false;

      Console.WriteLine("for variable \'a\'...");
      Console.WriteLine("default: " + a.GetType());
      Console.WriteLine("Name: " + a.GetType().Name);
      Console.WriteLine("FullName: " + a.GetType().FullName);
      Console.WriteLine("Namespace: " + a.GetType().Namespace);
      Console.WriteLine();

      Console.WriteLine("for variable \'b\'...");
      Console.WriteLine("default: " + b.GetType());
      Console.WriteLine("Name: " + b.GetType().Name);
      Console.WriteLine("FullName: " + b.GetType().FullName);
      Console.WriteLine("Namespace: " + b.GetType().Namespace);
      Console.WriteLine();

      Console.WriteLine("for variable \'c\'...");
      Console.WriteLine("default: " + c.GetType());
      Console.WriteLine("Name: " + c.GetType().Name);
      Console.WriteLine("FullName: " + c.GetType().FullName);
      Console.WriteLine("Namespace: " + c.GetType().Namespace);
      Console.WriteLine();

      student std = new student();
      Console.WriteLine("for structure \'std\'...");
      Console.WriteLine("default: " + std.GetType());
      Console.WriteLine("Name: " + std.GetType().Name);
      Console.WriteLine("FullName: " + std.GetType().FullName);
      Console.WriteLine("Namespace: " + std.GetType().Namespace);
      Console.WriteLine();

      //hit ENTER to exit
      Console.ReadLine();
    }
  }
}

Output

for variable 'a'...
default: System.Char
Name: Char
FullName: System.Char
Namespace: System

for variable 'b'...
default: System.Int32
Name: Int32
FullName: System.Int32
Namespace: System

for variable 'c'...
default: System.Boolean
Name: Boolean
FullName: System.Boolean
Namespace: System

for structure 'std'...
default: Test.Program+student
Name: student
FullName: Test.Program+student
Namespace: Test




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.