🎓 A1Learner
🔍
0 of 28 lessons completed

C# Conversions

A conversion operator teaches the compiler how to turn one type into another. An implicit conversion runs on a plain assignment, and an explicit conversion needs a cast.

An implicit conversion to int

The implicit operator returns the cents held by a Money value. A plain assignment to an int uses it without a cast.

using System;

class Program
{
    static void Main(string[] args)
    {
        Money money = new Money(150);
        int cents = money;
        Console.WriteLine(cents);
    }
}

class Money
{
    public int Cents;

    public Money(int cents)
    {
        Cents = cents;
    }

    public static implicit operator int(Money money)
    {
        return money.Cents;
    }
}

An explicit conversion with a cast

The explicit operator builds a Fahrenheit value from a Celsius value. The cast in the assignment makes the conversion visible.

using System;

class Program
{
    static void Main(string[] args)
    {
        Fahrenheit result = (Fahrenheit)new Celsius(100);
        Console.WriteLine(result.Degrees);
    }
}

class Celsius
{
    public int Degrees;

    public Celsius(int degrees)
    {
        Degrees = degrees;
    }

    public static explicit operator Fahrenheit(Celsius celsius)
    {
        return new Fahrenheit(celsius.Degrees * 9 / 5 + 32);
    }
}

class Fahrenheit
{
    public int Degrees;

    public Fahrenheit(int degrees)
    {
        Degrees = degrees;
    }
}

No conversion, no assignment

The Wallet class declares no conversion operator at all. The assignment to an int fails, and the compiler reports the error below.

using System;

class Program
{
    static void Main(string[] args)
    {
        Wallet wallet = new Wallet(150);
        int cents = wallet;
        Console.WriteLine(cents);
    }
}

class Wallet
{
    public int Cents;

    public Wallet(int cents)
    {
        Cents = cents;
    }
}

Example

using System;

class Program
{
    static void Main(string[] args)
    {
        Meters meters = new Feet(1250);
        Console.WriteLine(meters.Value);
        Feet feet = (Feet)new Meters(381);
        Console.WriteLine(feet.Value);
    }
}

class Meters
{
    public double Value;

    public Meters(double value)
    {
        Value = value;
    }

    public static explicit operator Feet(Meters meters)
    {
        return new Feet(meters.Value * 1250 / 381);
    }
}

class Feet
{
    public double Value;

    public Feet(double value)
    {
        Value = value;
    }

    public static implicit operator Meters(Feet feet)
    {
        return new Meters(feet.Value * 381 / 1250);
    }
}

Key points

  • A conversion operator is public and static, and it returns the target type.
  • An implicit operator suits a conversion that always succeeds and loses nothing.
  • An explicit operator suits a conversion that a reader should notice at the assignment.
  • The cast in an explicit conversion names the target type in parentheses.
  • A type pair with no conversion operator does not convert, and the compiler reports an error.

In this example, a Feet value converts to Meters through an implicit operator, so the assignment needs no cast. One foot is exactly 0.3048 meters, and the code writes that factor as 381 over 1250. A Meters value converts back to Feet through an explicit operator, so the cast appears in the code. The printed values are 381 meters and then 1250 feet.

Note

A conversion operator must sit inside one of the two types it connects.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

When does an implicit conversion operator run?