C# IConvertible Interface

Beginner
5 min read
Updated: Nov 2026
3 examples
Interface

Definition

In C#, IConvertible is an interface that lets a type say "I know how to turn myself into other basic types." Built-in types such as int, double, bool, string, and DateTime all implement it.

The Convert class uses it behind the scenes. It also gives you GetTypeCode(), which tells you which basic type a value is.

Syntax

You can call its methods through an IConvertible variable.

C#
IConvertible value = "123";
int number = value.ToInt32(null);
CallWhat it does
value.GetTypeCode()Returns the TypeCode for the value.
value.ToInt32(null)Converts the value to an int.
Convert.ToInt32(value)The simpler everyday helper that relies on IConvertible.

String exposes it too. See String.GetTypeCode().

Main Members

MemberDescription
GetTypeCode()Returns the TypeCode of the value.
ToBoolean(provider)Converts the value to bool.
ToInt32(provider)Converts the value to int.
ToDouble(provider)Converts the value to double.
ToString(provider)Converts the value to string.
ToType(type, provider)Converts the value to any requested Type.

C# Examples

Example 1 — Convert a String to int

Use the interface to turn "123" into a number.

C#
using System;

class Program {
    static void Main() {
        IConvertible text = "123";

        int number = text.ToInt32(null);

        Console.WriteLine(number + 1);
    }
}

How It Works

  1. Store as IConvertible. A string implements the interface.
  2. ToInt32(null). Parses the text. null means use the current culture.
  3. Math works. Because the result is an int, number + 1 is 124.

Example 2 — Convert Mixed Values

Loop over different values and convert each to an int.

C#
using System;

class Program {
    static void Main() {
        object[] values = { "42", 3.9, true };

        foreach (object v in values) {
            if (v is IConvertible c) {
                Console.WriteLine($"{v} -> {c.ToInt32(null)}");
            }
        }
    }
}

How It Works

  1. Check the interface. v is IConvertible c keeps only convertible values.
  2. Rounding. A double is rounded when converted to int.
  3. bool. true converts to 1.

Example 3 — Which Types Implement IConvertible?

Check types with typeof and IsAssignableFrom().

C#
using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        Type target = typeof(IConvertible);

        Console.WriteLine(target.IsAssignableFrom(typeof(string)));
        Console.WriteLine(target.IsAssignableFrom(typeof(int)));
        Console.WriteLine(target.IsAssignableFrom(typeof(List<int>)));
    }
}

How It Works

  1. Basic types say yes. string and int implement the interface.
  2. Collections say no. List<int> does not.
  3. Safe check. Test first, then convert, to avoid exceptions.

Frequently Asked Questions

It is an interface that lets a type convert itself to basic types like int, double, bool, and string.
Built-in types such as int, double, bool, char, string, and DateTime.
Convert methods such as Convert.ToInt32() use IConvertible when they work with an object.
An IFormatProvider that controls culture. Pass null to use the current culture.
Rarely. Most programs just use Convert or the parse methods. Implementing it is mainly for advanced scenarios.

Next: typeof

Learn how to get a Type object from a type name.

typeof tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

9 people found this page helpful