In C#, Convert.ChangeType converts a value to a target Type or TypeCode chosen at runtime.
The source must implement IConvertible (most primitive types do). Prefer the strongly typed ToInt32/ToString helpers when you already know the destination.
This is a static member of System.Convert. You call it with the type name, such as Convert.ChangeType(...). Convert is the everyday API for converting between common CLR types in C#.
Syntax
Pass the value and a destination type or TypeCode.
C#
object n = Convert.ChangeType("42", typeof(int));
object s = Convert.ChangeType(99, TypeCode.String);
int i = (int)Convert.ChangeType("7", typeof(int));
Call
What it does
Convert.ChangeType("42", typeof(int))
String → boxed int.
Convert.ChangeType(99, TypeCode.String)
Int → boxed string via TypeCode.
Returns object—cast to the real type. Invalid conversions throw InvalidCastException, FormatException, or OverflowException.
Library declarations
C#
public static object? ChangeType(object? value, Type conversionType);
public static object? ChangeType(object? value, TypeCode typeCode);
Parameters
Parameter
Description
value
Required. An IConvertible value (or null in some cases).
conversionType / typeCode
Required. Destination Type or TypeCode.
Return Value
Return Value
Description
object
Boxed converted value—cast before use.
C# Examples
Example 1 — Change String to Int
Use typeof to pick the destination at runtime.
C#
using System;
class Program {
static void Main() {
object boxed = Convert.ChangeType("42", typeof(int));
int n = (int)boxed;
Console.WriteLine(n);
Console.WriteLine(n.GetType().Name);
}
}
Output
42
Int32
How It Works
Target typeof(int). Destination chosen as a Type object.
Boxed result. ChangeType returns object.
Cast back. (int) unwraps the value.
Type name. Confirms Int32.
Example 2 — TypeCode.String Destination
Convert with a TypeCode instead of typeof.
C#
using System;
class Program {
static void Main() {
object text = Convert.ChangeType(99, TypeCode.String);
Console.WriteLine(text);
Console.WriteLine(text is string);
}
}
Output
99
True
How It Works
TypeCode.String. Asks for a string result.
99 → "99". Printed as text.
is string. Runtime check succeeds.
Enum target. TypeCode is handy in switch-style code.
Example 3 — ChangeType to Boolean
Convert digit text into a bool via ChangeType.
C#
using System;
class Program {
static void Main() {
object flag = Convert.ChangeType("True", typeof(bool));
Console.WriteLine(flag);
Console.WriteLine((bool)flag);
}
}
Output
True
True
How It Works
Bool typeof. Destination is System.Boolean.
True text. Same rules as ToBoolean for strings.
Print then cast. Shows object and bool forms.
Prefer ToBoolean. When you know the target ahead of time.
Frequently Asked Questions
When the destination type is chosen at runtime (plugins, serializers, TypeCode switches).
IConvertible—most primitives do.
ChangeType returns object; cast to the real CLR type.
It adds indirection; prefer typed To* helpers when possible.
InvalidCastException, FormatException, OverflowException depending on the values.