C# Academy · Lesson

Implicit and Explicit Conversions

Convert between compatible types safely.

Lesson 3 of 413 steps

Implicit and Explicit Conversions is a free C# Academy lesson on CoddyKit. This is lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 4 lessons in total.

Converting Between Types

C# can convert one numeric type to another. Some conversions happen automatically (implicit), while others need an explicit cast.

using System;

class Program
{
    static void Main()
    {
        int i = 10;
        double d = i;       // implicit
        int back = (int)d;   // explicit
        Console.WriteLine(d + ", " + back);
    }
}

Implicit (Widening) Conversions

An implicit conversion is allowed when no data can be lost, such as int to long or int to double. No cast is needed.

using System;

class Program
{
    static void Main()
    {
        int i = 42;
        long l = i;      // widening, safe
        double d = i;    // widening, safe
        Console.WriteLine(l + ", " + d);
    }
}

Why Widening Is Safe

The target type can represent every value of the source type, so the compiler permits it silently.

using System;

class Program
{
    static void Main()
    {
        byte b = 200;
        int i = b;       // byte fits in int
        Console.WriteLine("byte " + b + " -> int " + i);
    }
}

Explicit (Narrowing) Conversions

An explicit conversion (a cast) is required when data might be lost, such as double to int. You write (int)value.

using System;

class Program
{
    static void Main()
    {
        double d = 9.78;
        int i = (int)d;   // truncates toward zero -> 9
        Console.WriteLine(i);
    }
}

Casting Truncates, Not Rounds

Casting a floating value to an integer drops the fractional part; it does not round. Use Math.Round first if you want rounding.

using System;

class Program
{
    static void Main()
    {
        double d = 7.99;
        Console.WriteLine("Cast: " + (int)d);
        Console.WriteLine("Round: " + (int)Math.Round(d));
    }
}

Overflow on Narrowing

If a value does not fit the target type, a plain cast may wrap around silently. Wrap it in checked to throw on overflow instead.

using System;

class Program
{
    static void Main()
    {
        int big = 300;
        byte wrapped = (byte)big;   // 300 wraps to 44
        Console.WriteLine("Wrapped: " + wrapped);

        try
        {
            byte safe = checked((byte)big);
            Console.WriteLine(safe);
        }
        catch (OverflowException)
        {
            Console.WriteLine("checked caught overflow");
        }
    }
}

Loss of Precision

Even some widening conversions, like very large long to float, can lose precision though they are still implicit.

using System;

class Program
{
    static void Main()
    {
        long big = 9007199254740993L;
        double approx = big;   // implicit, may lose precision
        Console.WriteLine("long: " + big);
        Console.WriteLine("double: " + approx.ToString("F0"));
    }
}

char and Numeric Conversions

A char implicitly converts to its numeric code, and you can explicitly cast a number back to a char.

using System;

class Program
{
    static void Main()
    {
        char c = 'A';
        int code = c;          // implicit -> 65
        char next = (char)(code + 1);   // explicit -> B
        Console.WriteLine(code + " -> " + next);
    }
}

Conversions in Expressions

In mixed arithmetic, smaller types are promoted to the larger type automatically before the operation.

using System;

class Program
{
    static void Main()
    {
        int a = 7;
        double b = 2;
        double result = a / b;   // a promoted to double -> 3.5
        Console.WriteLine(result);
    }
}

Integer Division Pitfall

If both operands are integers, division is integer division. Cast one to double first to get a fractional result.

using System;

class Program
{
    static void Main()
    {
        int a = 7, b = 2;
        Console.WriteLine("int / int: " + (a / b));
        Console.WriteLine("with cast: " + ((double)a / b));
    }
}

Putting It Together

Prefer implicit conversions when safe, use explicit casts when narrowing, and watch for truncation, overflow, and integer division.

using System;

class Program
{
    static void Main()
    {
        int total = 17, count = 5;
        double average = (double)total / count;   // cast to avoid integer division
        int rounded = (int)Math.Round(average);
        Console.WriteLine("avg=" + average + ", rounded=" + rounded);
    }
}

Quick Check

Test your understanding of conversions.

Recap

Implicit (widening) conversions happen automatically when no data is lost (int to long, int to double). Explicit (narrowing) conversions need a cast like (int)x and may truncate or overflow. Use checked to catch overflow and cast to double to avoid integer division.

using System;

class Program
{
    static void Main()
    {
        double d = 3.9;
        int i = (int)d;     // explicit, truncates to 3
        long l = i;          // implicit widening
        Console.WriteLine(i + ", " + l);
    }
}
Free to start

Learn C# with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
93
Lessons
346

Frequently Asked Questions

Is the “Implicit and Explicit Conversions” lesson free?

Yes — the full text of “Implicit and Explicit Conversions” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 4 lessons in total.

What will I learn in “Implicit and Explicit Conversions”?

Convert between compatible types safely. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Implicit and Explicit Conversions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Value Types vs Reference Types
  2. Boxing and Unboxing
  3. Implicit and Explicit Conversions
  4. The Convert Class and Parsing
← Back to C# Academy