The instance method GetType() returns a System.Type object that describes what kind of object you have at runtime. On any string, it reports System.String—regardless of whether the text is "Hello" or "C#". The method is inherited from Object, not unique to strings.
01
From Object
Inherited method.
02
System.Type
Return type.
03
Runtime Info
Actual instance type.
04
vs typeof()
Compile-time type.
05
Type Properties
Name, FullName.
06
vs GetTypeCode
Different method.
Fundamentals
Definition and Usage
In C#, GetType() is defined on System.Object and therefore available on every string. It answers: “What is the runtime type of this object?” For string literals and variables, the answer is always System.String.
The returned Type object exposes metadata—name, namespace, base type, interfaces, methods, and more. This is the foundation of reflection, used in logging, serialization, dependency injection, and generic utilities that adapt to different types at runtime.
💡
Beginner Tip
GetType() describes the type, not the string content. "abc".GetType() and "xyz".GetType() both return System.String. For type-category checks on primitives, see GetTypeCode() on the next tutorial page.
Foundation
📝 Syntax
Declared on System.Object (inherited by String):
C#
public Type GetType();
Parameters
None.
Return Value
A System.Type instance representing the runtime type of the object. Common properties include Name, FullName, Namespace, and BaseType.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Runtime type of string
Type t = text.GetType()
Print type name
text.GetType().Name → "String"
Full type name
text.GetType().FullName → "System.String"
Compile-time String type
typeof(string)
Simple type test
obj is string
Instance
text.GetType()
Runtime
typeof
typeof(string)
Compile-time
Name
.Name → "String"
Short name
is check
obj is string
Preferred test
Hands-On
Examples Gallery
Run with dotnet run. Each example shows how GetType() works on strings—from basic type retrieval to metadata, comparison with typeof, and type checking patterns.
📚 Getting Started
Get the runtime type of a string instance.
Example 1 — Basic GetType() Usage
Obtain and print the Type of a string variable.
C#
using System;
class Program {
static void Main() {
string sampleString = "Hello, C#!";
Type stringType = sampleString.GetType();
Console.WriteLine($"Type of the string: {stringType}");
}
}
📤 Output:
Type of the string: System.String
How It Works
GetType() inspects the object in memory and returns its runtime type. The text content "Hello, C#!" does not change the type—it is still System.String.
Example 2 — Explore Type Properties
Read metadata from the returned Type object.
C#
using System;
class Program {
static void Main() {
string text = "C#";
Type t = text.GetType();
Console.WriteLine($"Name: {t.Name}");
Console.WriteLine($"FullName: {t.FullName}");
Console.WriteLine($"Namespace: {t.Namespace}");
Console.WriteLine($"BaseType: {t.BaseType?.Name}");
}
}
📤 Output:
Name: String
FullName: System.String
Namespace: System
BaseType: Object
How It Works
Type exposes rich metadata. String lives in the System namespace and inherits from Object. Reflection APIs use these properties to discover methods and members dynamically.
📈 Practical Patterns
typeof comparison, polymorphism, and type checking.
Example 3 — GetType() vs typeof(string)
Compare runtime instance type with compile-time type metadata.
C#
using System;
class Program {
static void Main() {
string text = "Hello";
Type viaGetType = text.GetType();
Type viaTypeof = typeof(string);
Console.WriteLine($"GetType(): {viaGetType.FullName}");
Console.WriteLine($"typeof(): {viaTypeof.FullName}");
Console.WriteLine($"Are equal: {viaGetType == viaTypeof}");
}
}
📤 Output:
GetType(): System.String
typeof(): System.String
Are equal: True
How It Works
For a string instance, both approaches refer to System.String. Use typeof when you know the type at compile time. Use GetType() on a variable when the actual runtime type might differ (e.g. an object holding different types).
Example 4 — GetType() Through an object Variable
When a string is stored as object, GetType() still reveals the real runtime type.
C#
using System;
class Program {
static void Main() {
object value = "Stored as object";
object number = 42;
Console.WriteLine($"value type: {value.GetType().Name}");
Console.WriteLine($"number type: {number.GetType().Name}");
}
}
📤 Output:
value type: String
number type: Int32
How It Works
The compile-time type of both variables is object, but GetType() returns the actual runtime type—String for the first, Int32 for the second. This is why GetType() matters in polymorphic code.
Example 5 — Type Checking: is vs GetType()
Prefer is for simple checks; use exact GetType() comparison when needed.
C#
using System;
class Program {
static void Main() {
object? data = "Hello";
bool viaIs = data is string;
bool viaGetType = data?.GetType() == typeof(string);
Console.WriteLine($"is string: {viaIs}");
Console.WriteLine($"GetType() == typeof: {viaGetType}");
data = null;
Console.WriteLine($"null is string: {data is string}");
// data.GetType() would throw on null
}
}
📤 Output:
is string: True
GetType() == typeof: True
null is string: False
How It Works
is string returns false for null without throwing. Calling GetType() on null throws NullReferenceException. For everyday string checks, is or pattern matching is safer and clearer.
Applications
🚀 Common Use Cases
Logging and debugging — print the runtime type of a value when diagnosing issues.
Reflection — discover methods and properties dynamically from a Type.
Generic utilities — branch behavior based on the actual type of an object parameter.
Serialization frameworks — inspect types to choose formatters or converters.
Learning Object model — understand how all C# types inherit GetType() from Object.
🧠 How GetType() Works
1
You call on an instance
text.GetType() on any non-null object.
Invoke
2
Runtime reads object header
CLR looks up the actual type stored with the object in memory.
Lookup
3
System.Type metadata returned
Name, methods, base type, interfaces, and more are available.
Metadata
=
🔍
System.String for strings
Content does not matter—every string instance reports System.String.
Important
📝 Notes
GetType() is inherited from Object—not a string-specific override.
Calling on null throws NullReferenceException.
String content does not affect the returned type—always System.String.
GetType() differs from GetTypeCode(), which returns a TypeCode enum value.
typeof(string) is compile-time; GetType() is runtime on an instance.
String is sealed—no subclasses, so exact type checks are straightforward.
Performance
⚡ Optimization
GetType() is a lightweight CLR operation suitable for occasional use. Avoid calling it on every iteration of a hot loop if the type is already known. Prefer is pattern matching for type checks in performance-sensitive paths. Cache Type references (e.g. typeof(string)) when used repeatedly.
Wrap Up
Conclusion
The C# GetType() method exposes runtime type information through System.Type. On strings it always reports System.String, but the same API works on every object and is essential for reflection and dynamic code.
Practice the examples above, distinguish GetType() from typeof and GetTypeCode(), then continue to GetTypeCode() for enum-based type classification.
Use typeof(string) when the type is known at compile time
Null-check before calling GetType()
Explore Type properties for reflection scenarios
Remember content ≠ type for strings
❌ Don’t
Call GetType() on a null reference
Confuse GetType() with GetTypeCode()
Expect different strings to report different types
Assume GetType() is defined only on String
Overuse runtime type checks when generics or is suffice
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about GetType()
Use these points whenever you inspect types in C#.
5
Core concepts
🔍01
System.Type
Runtime metadata.
Return
🛠02
From Object
All types have it.
Origin
📄03
System.String
For all strings.
Result
📝04
vs typeof
Runtime vs compile.
Compare
✅05
use is
Safer checks.
Pattern
❓ Frequently Asked Questions
GetType() returns a System.Type object describing the runtime type of the instance. For any normal string variable, it returns typeof(System.String)—the type is System.String regardless of the text content inside the string.
Type t = text.GetType(); No parameters. It is an instance method inherited from System.Object, so every string supports it.
It returns System.Type with metadata such as Name ("String"), FullName ("System.String"), Namespace ("System"), and BaseType ("System.Object"). Use these properties to inspect the type at runtime.
GetType() is called on an instance and returns the actual runtime type of that object. typeof(string) is evaluated at compile time and always refers to the System.String type. For a string instance, both yield System.String.
No. GetType() is inherited from System.Object—the base class of all C# types. String does not define its own special version; it uses the Object implementation.
Prefer "obj is string" for simple type tests—it handles null safely and supports inheritance. Use GetType() == typeof(string) only when you need an exact runtime type match (no subclasses). For strings (sealed class), both behave similarly for non-null values.
Did you know?
Every C# type ultimately inherits from System.Object, which provides GetType(), Equals(), GetHashCode(), and ToString(). When you call these on a string, you are using the string’s overrides (where they exist) or the inherited Object behavior—GetType() itself is not overridden by String.