C# String GetTypeCode() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In C#, GetTypeCode() returns a TypeCode enum category. For any non-null string, the result is TypeCode.String.

You call it with no parameters. The text content does not change the result. Use it when you need a simple category for a switch.

Syntax

Call GetTypeCode on the string. Start with this line.

C#
TypeCode code = text.GetTypeCode();
CallWhat it does
text.GetTypeCode()Returns TypeCode.String for a non-null string.

GetTypeCode is a lightweight category. GetType returns full System.Type metadata.

Library declarations
C#
public TypeCode GetTypeCode();

Parameters

This method has no parameters.

Return Value

Return ValueDescription
TypeCode.StringThe category for any non-null string.

C# Examples

Example 1 — Always TypeCode.String

Any non-null string returns TypeCode.String, regardless of content.

C#
using System;

class Program {
    static void Main() {
        string a = "Hello";
        string b = "C#";

        Console.WriteLine(a.GetTypeCode());
        Console.WriteLine(b.GetTypeCode());
        Console.WriteLine(a.GetTypeCode() == TypeCode.String);
    }
}

How It Works

  1. Call on each string. GetTypeCode() returns a TypeCode enum.
  2. Same category. Different text still maps to TypeCode.String.
  3. Print. Writing the enum shows the name String.
  4. Compare. == TypeCode.String is the usual check.

Example 2 — GetTypeCode() vs GetType()

Use GetTypeCode for a category enum; use GetType when you need full type metadata.

C#
using System;

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

        TypeCode code = text.GetTypeCode();
        Type type = text.GetType();

        Console.WriteLine($"TypeCode: {code}");
        Console.WriteLine($"Type:     {type.FullName}");
        Console.WriteLine($"Static:   {Type.GetTypeCode(type)}");
    }
}

How It Works

  1. GetTypeCode. Lightweight enum: String.
  2. GetType. Full System.Type with FullName and more.
  3. Static helper. Type.GetTypeCode(type) maps a Type to the same enum.
  4. Pick one. Switch on categories → GetTypeCode; inspect members → GetType.

Example 3 — Switch on TypeCode

Classify mixed IConvertible values with a simple switch.

C#
using System;

class Program {
    static void Describe(IConvertible value) {
        switch (value.GetTypeCode()) {
            case TypeCode.String:
                Console.WriteLine($"text: {value}");
                break;
            case TypeCode.Int32:
                Console.WriteLine($"int:  {value}");
                break;
            default:
                Console.WriteLine($"other: {value.GetTypeCode()}");
                break;
        }
    }

    static void Main() {
        Describe("Hi");
        Describe(42);
    }
}

How It Works

  1. IConvertible parameter. Strings and ints both implement it.
  2. Switch on GetTypeCode. Branches by category, not by full type name.
  3. String case. "Hi" hits TypeCode.String.
  4. Int case. 42 hits TypeCode.Int32.

Frequently Asked Questions

GetTypeCode() returns TypeCode.String for any non-null string.
No. Call it on the string with empty parentheses.
TypeCode.String. The text content does not change that.
GetTypeCode returns a TypeCode enum. GetType returns full System.Type metadata.
Calling GetTypeCode() on null throws NullReferenceException.

Next: C# Interview Programs

Practice with hands-on C# interview questions.

Interview programs →

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