0Pricing
C# Academy · Lesson

Suffixes in practice; casting vs conversion; C# 6 object creation

Learn numeric suffixes in practice, the difference between casting and conversion, and how to write C# 6-friendly object creation (no target-typed new).

Suffixes in practice; casting vs conversion; C# 6 object creation is a free C# Academy lesson on CoddyKit — lesson 3 of 3. 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, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Lesson overview

Goal: Use numeric suffixes correctly, know when to cast vs convert, and create objects in C# 6 without target-typed new.

Suffixes in practice

Suffixes: use f for float (3.14f), m for decimal (10m), L for long (123L). Choose types for precision and range.

Casting basics

Casting changes how the value is viewed by the compiler; explicit casts may lose data.

using System;

public class Program
{
  public static void Main(string[] args)
  {
    // Implicit numeric promotion (safe)
    int i = 42;
    double d = i; // implicit cast to double
    Console.WriteLine("d = " + d);

    // Explicit cast (possible data loss)
    double pi = 3.99;
    int n = (int)pi; // truncates
    Console.WriteLine("n = " + n);
  }
}

Conversion basics

Conversion transforms data: TryParse is safest; Parse/Convert can throw on bad input.

using System;

public class Program
{
  public static void Main(string[] args)
  {
    string text = "123";
    int value;

    // TryParse: safe conversion (no exception)
    bool ok = int.TryParse(text, out value);
    Console.WriteLine("ok=" + ok + " value=" + value);

    // Convert: throws if invalid
    int v2 = Convert.ToInt32(3.7); // rounds to 4
    Console.WriteLine("v2=" + v2);

    // Parse: throws if invalid
    int v3 = int.Parse("42");
    Console.WriteLine("v3=" + v3);
  }
}

C# 6 object creation

No target-typed new in C# 6: write the type explicitly (left or constructor). Avoid newer syntax for older compilers.

using System;

// Simple point class
public class Point
{
  public int X;
  public int Y;
}

public class Program
{
  public static void Main(string[] args)
  {
    // C# 6 style: specify the type on the left or right
    Point p1 = new Point();
    p1.X = 1; p1.Y = 2;

    var p2 = new Point(); // type inferred from constructor
    p2.X = 3; p2.Y = 4;

    Console.WriteLine("p1=(" + p1.X + "," + p1.Y + ")");
    Console.WriteLine("p2=(" + p2.X + "," + p2.Y + ")");
  }
}

All together

Compare cast vs conversion results and see suffixes in one place. Prefer TryParse for user input.

using System;

public class Program
{
  public static void Main(string[] args)
  {
    // Suffixes
    float f = 2.5f;
    decimal price = 9.99m;
    long big = 10000000000L;
    Console.WriteLine("f=" + f + " price=" + price + " big=" + big);

    // Cast vs Convert
    double d = 5.8;
    int viaCast = (int)d;              // 5 (truncate)
    int viaConvert = Convert.ToInt32(d); // 6 (round)
    Console.WriteLine("viaCast=" + viaCast + " viaConvert=" + viaConvert);

    // TryParse safety
    int n;
    bool ok = int.TryParse("oops", out n);
    Console.WriteLine("TryParse ok=" + ok + " n=" + n);
  }
}

Cast vs convert

Quick check: Which statement is true about casting vs conversion in C#?

Recap

Recap: Use f/m/L suffixes, cast only when safe, convert when you need a new representation (prefer TryParse). In C# 6, declare types explicitly—no target-typed new.

Frequently asked questions

Is the “Suffixes in practice; casting vs conversion; C# 6 object creation” lesson free?

Yes — the full text of “Suffixes in practice; casting vs conversion; C# 6 object creation” is free to read here on the web, and the C# Academy course includes 3 lessons in total. 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.

What will I learn in “Suffixes in practice; casting vs conversion; C# 6 object creation”?

Learn numeric suffixes in practice, the difference between casting and conversion, and how to write C# 6-friendly object creation (no target-typed new). 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; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Suffixes in practice; casting vs conversion; C# 6 object creation” 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 vs reference; literals; var inference; const/readonly
  2. Operators & expressions: arithmetic, comparison, logical
  3. Suffixes in practice; casting vs conversion; C# 6 object creation
← Back to C# Academy