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();
Call
What 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 Value
Description
TypeCode.String
The 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);
}
}
Output
String
String
True
How It Works
Call on each string.GetTypeCode() returns a TypeCode enum.
Same category. Different text still maps to TypeCode.String.
Print. Writing the enum shows the name String.
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)}");
}
}