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();
Call
What 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 Value
Description
System.Type
Runtime 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}");
}
}