C# typeof Operator

Beginner
5 min read
Updated: Nov 2026
3 examples
Operator

Definition

In C#, the typeof operator returns a System.Type object for a type you name, such as int or string. The answer is known at compile time.

A Type object lets you read a type's name, check what it inherits from, and compare it with another type.

Syntax

Put the type name inside the parentheses.

C#
Type t = typeof(string);
CallWhat it does
typeof(string)The Type object for string.
obj.GetType() == typeof(string)Checks whether obj is exactly a string.
typeof(List<>)An open generic type with no type argument.

Also see String.GetType() for the run-time version.

Useful Type Properties

ExpressionResult
typeof(int).NameInt32
typeof(int).FullNameSystem.Int32
typeof(string).NameString
typeof(int[]).NameInt32[]
typeof(List<int>).NameList`1

C# Examples

Example 1 — Print Type Names

Read the name of a few common types.

C#
using System;

class Program {
    static void Main() {
        Console.WriteLine(typeof(int).Name);
        Console.WriteLine(typeof(int).FullName);
        Console.WriteLine(typeof(string).Name);
    }
}

How It Works

  1. int is Int32. The C# keyword int is a shortcut for System.Int32.
  2. FullName includes the namespace. That is why it prints System.Int32.
  3. Name is short. Just the type name without the namespace.

Example 2 — Compare with GetType()

Check the real type of an object at run time.

C#
using System;

class Program {
    static void Main() {
        object value = "hello";

        Console.WriteLine(value.GetType() == typeof(string));
        Console.WriteLine(value.GetType() == typeof(int));
    }
}

How It Works

  1. GetType() looks at the object. It tells you the real type right now.
  2. typeof names a type. It gives the Type to compare against.
  3. Equal types. A string is a string, so the first line is True.

Example 3 — typeof with Arrays and Generics

See how arrays, generics, and your own classes appear.

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Console.WriteLine(typeof(int[]).Name);
        Console.WriteLine(typeof(List<int>).Name);
        Console.WriteLine(typeof(Program).Name);
    }
}

How It Works

  1. Arrays. The name ends with [].
  2. Generics. List`1 means a generic type with one type argument.
  3. Your own class. typeof(Program) works for classes you write.

Frequently Asked Questions

It returns the System.Type object for a type name, such as typeof(int) or typeof(string).
typeof takes a type name at compile time. GetType() is called on an object and returns its real type at run time.
No. typeof needs a type name. Use variable.GetType() for a variable.
It returns Int32, because int is an alias for System.Int32.
Use it to compare types, read type names, or pass a Type to methods such as Activator or reflection APIs.

Next: is Operator

Check an object's type and pull out its value in one step.

is operator tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful