C# String GetType() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, GetType() returns a System.Type object for the runtime type of the instance. For a normal string, that type is System.String.

You call it with no parameters. The text inside the string does not change the type. Calling it on null throws.

Syntax

Call GetType on the string. Start with this line.

C#
Type t = text.GetType();
CallWhat it does
text.GetType()Returns System.Type metadata for System.String.

Prefer is string for a simple type check. Use GetType when you need Type metadata such as Name or FullName.

Library declarations
C#
public Type GetType();

Parameters

This method has no parameters.

Return Value

Return ValueDescription
System.TypeRuntime type metadata. For strings this is System.String.

C# Examples

Example 1 — Name and FullName

Call GetType() on a string and print the common Type properties.

C#
using System;

class Program {
    static void Main() {
        string text = "Hello";
        Type t = text.GetType();

        Console.WriteLine($"Name:     {t.Name}");
        Console.WriteLine($"FullName: {t.FullName}");
        Console.WriteLine($"Namespace:{t.Namespace}");
    }
}

How It Works

  1. Instance call. text.GetType() asks the runtime for the object’s type.
  2. Type object. You get a System.Type, not a string of the text.
  3. Name vs FullName. Short name is String; full is System.String.
  4. Content ignored. Any non-null string yields the same type metadata.

Example 2 — GetType() vs typeof()

Compare the instance result with the compile-time typeof(string) token.

C#
using System;

class Program {
    static void Main() {
        string text = "Hi";

        Type fromInstance = text.GetType();
        Type fromTypeof = typeof(string);

        Console.WriteLine($"GetType:  {fromInstance.FullName}");
        Console.WriteLine($"typeof:   {fromTypeof.FullName}");
        Console.WriteLine($"Same?     {fromInstance == fromTypeof}");
    }
}

How It Works

  1. GetType. Needs a live instance; returns that object’s runtime type.
  2. typeof. Needs a type name at compile time—no instance required.
  3. Compare. For a normal string, both point at System.String.
  4. When they differ. On other types (polymorphism), GetType can return a subclass while the variable’s declared type is a base class.

Example 3 — Type Check Patterns

Prefer is for everyday checks; use GetType() == typeof(...) for an exact match.

C#
using System;

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

        bool withIs = value is string;
        bool withGetType = value != null
            && value.GetType() == typeof(string);

        Console.WriteLine($"is string?     {withIs}");
        Console.WriteLine($"GetType match? {withGetType}");
    }
}

How It Works

  1. Boxed as object. value holds a string at runtime.
  2. is string. Null-safe and idiomatic for “compatible type?” checks.
  3. Exact GetType match. Compares runtime type to typeof(string).
  4. Null guard. Never call GetType() on null—check first.

Frequently Asked Questions

GetType() returns System.Type metadata for the runtime type of the instance.
No. Call it on the string with empty parentheses.
A Type object for System.String, no matter what text is inside.
GetType() runs on an instance. typeof(string) is known at compile time. For a string instance, both refer to System.String.
Calling GetType() on null throws NullReferenceException.

Next: GetTypeCode()

Learn how strings report TypeCode.String.

GetTypeCode() tutorial →

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