0Pricing
C# Academy · Lesson

Value vs reference; literals; var inference; const/readonly

Learn how value types differ from reference types, how literals and numeric suffixes work, when to use var, and the roles of const and readonly.

Value vs reference; literals; var inference; const/readonly is a free C# Academy lesson on CoddyKit — lesson 1 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.

Why types matter

Goal: Understand value vs reference types and how variables, literals, and constants behave in everyday code.

Value vs reference

Value type: copies data on assignment (e.g., int, bool, struct). Reference type: copies a reference (class, array, string).

  • Value: safer local changes
  • Reference: shared object identity

Literals & suffixes

Literals have types. Use suffixes for clarity: f (float), m (decimal), L (long).

  • 3.14f → float
  • 10m → decimal (money)
  • 123L → long

var inference

var lets the compiler infer the type from the right-hand side. Use it when the type is obvious; otherwise be explicit.

const vs readonly

const: compile-time constant; readonly: set in constructor or at declaration.

  • Use const for fixed values (e.g., Pi)
  • Use readonly for values known at runtime

Types in practice

See value vs reference behavior, numeric suffixes, simple var inference, and const/readonly in one small program.

using System;

// Value type (struct) copies by value
public struct PointS
{
  public int X;
  public int Y;
}

// Reference type (class) copies the reference
public class PointC
{
  public int X;
  public int Y;
}

// Readonly demo class
public class Config
{
  public readonly string Name;
  public Config(string name)
  {
    Name = name; // can set here
  }
}

public class Program
{
  // Compile-time constant
  private const double Pi = 3.14159;

  public static void Main(string[] args)
  {
    // 1) Value vs reference
    PointS a = new PointS { X = 1, Y = 2 };
    PointS b = a;         // copy value
    b.X = 99;
    Console.WriteLine("Value struct a.X = " + a.X); // 1 (unchanged)
    Console.WriteLine("Value struct b.X = " + b.X); // 99

    PointC ac = new PointC { X = 1, Y = 2 };
    PointC bc = ac;       // copy reference
    bc.X = 77;
    Console.WriteLine("Ref class ac.X = " + ac.X);  // 77 (same object)
    Console.WriteLine("Ref class bc.X = " + bc.X);  // 77

    // 2) Literals & suffixes
    float f = 3.14f;
    decimal price = 10m;
    long big = 123L;
    Console.WriteLine("f=" + f + " price=" + price + " big=" + big);

    // 3) var inference (type is obvious)
    var sum = a.X + a.Y; // int
    Console.WriteLine("sum=" + sum);

    // 4) const vs readonly
    Config cfg = new Config("Demo");
    Console.WriteLine("Pi=" + Pi + " Name=" + cfg.Name);
  }
}

Value type definition

Quick check: Which statement best describes a value type?

Recap

Recap: Value types copy data; reference types share objects. Use suffixes for numeric literals, var when clear, and choose const vs readonly appropriately.

Frequently asked questions

Is the “Value vs reference; literals; var inference; const/readonly” lesson free?

Yes — the full text of “Value vs reference; literals; var inference; const/readonly” 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 “Value vs reference; literals; var inference; const/readonly”?

Learn how value types differ from reference types, how literals and numeric suffixes work, when to use var, and the roles of const and readonly. 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 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Value vs reference; literals; var inference; const/readonly” 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