C# Convert ChangeType() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Convert

Definition

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));
CallWhat 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

ParameterDescription
valueRequired. An IConvertible value (or null in some cases).
conversionType / typeCodeRequired. Destination Type or TypeCode.

Return Value

Return ValueDescription
objectBoxed 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);
    }
}

How It Works

  1. Target typeof(int). Destination chosen as a Type object.
  2. Boxed result. ChangeType returns object.
  3. Cast back. (int) unwraps the value.
  4. 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);
    }
}

How It Works

  1. TypeCode.String. Asks for a string result.
  2. 99 → "99". Printed as text.
  3. is string. Runtime check succeeds.
  4. 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);
    }
}

How It Works

  1. Bool typeof. Destination is System.Boolean.
  2. True text. Same rules as ToBoolean for strings.
  3. Print then cast. Shows object and bool forms.
  4. 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.

Next: FromBase64CharArray()

Decode Base64 characters from a char[] slice into bytes with System.Convert.

FromBase64CharArray() tutorial →

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.

7 people found this page helpful