0Pricing
C# Academy · Lesson

Fields, auto-properties, object initializers

Define fields and auto-properties, and create objects with concise object initializers.

Fields, auto-properties, object initializers 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.

Class basics overview

Goal: Build simple classes.

  • Fields hold raw data
  • Auto-properties expose data cleanly
  • Object initializers set values at creation

Fields in a class

Fields are variables inside a class. For public data, properties are safer.

using System;

// Fields store raw data inside a class
public class Player
{
  public string name; // field (usually prefer properties for public APIs)
  public int level;
}

public class Program
{
  public static void Main(string[] args)
  {
    Player p = new Player();
    p.name = "Ada";
    p.level = 1;
    Console.WriteLine("Player " + p.name + " level " + p.level);
  }
}

Auto-properties

Auto-properties expose data via get/set without writing a field. You can set simple defaults.

using System;

// Auto-properties generate a hidden backing field
public class Book
{
  public string Title { get; set; }
  public string Author { get; set; }
  public int Pages { get; set; } = 0; // auto-property initializer (C# 6)
}

public class Program
{
  public static void Main(string[] args)
  {
    Book b = new Book();
    b.Title = "Clean Code";
    b.Author = "Robert C. Martin";
    b.Pages = 464;
    Console.WriteLine(b.Title + " by " + b.Author + " (" + b.Pages + " pages)");
  }
}

Object initializers

Object initializers set properties at creation time, keeping code short and clear.

using System;

public class Person
{
  public string Name { get; set; }
  public int Age { get; set; }
}

public class Program
{
  public static void Main(string[] args)
  {
    // Object initializer calls set-accessors after construction
    Person p = new Person { Name = "Alan", Age = 30 };
    Console.WriteLine(p.Name + " (" + p.Age + ")");
  }
}

Fields vs properties

Guideline:

  • Use private fields for internal storage.
  • Expose public properties so you can add validation later without breaking callers.
  • Keep names clear and simple.

Fields + properties together

Combine a private field with a property to control writes (simple invariants).

using System;

// Private fields + public properties pattern
public class Product
{
  private int _stock;                   // field (internal storage)
  public string Name { get; set; }      // property (public API)
  public int Stock                      // property with simple guard
  {
    get { return _stock; }
    set
    {
      if (value < 0) value = 0; // simple safety for beginners
      _stock = value;
    }
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Product p = new Product { Name = "Pen", Stock = 5 };
    p.Stock = -3; // setter clamps to 0
    Console.WriteLine(p.Name + " stock = " + p.Stock);
  }
}

Auto-property concept

Quick check: What is an auto-property in C#?

Recap

Recap: Define data with fields and expose it via auto-properties. Use object initializers to set values at creation.

Frequently asked questions

Is the “Fields, auto-properties, object initializers” lesson free?

Yes — the full text of “Fields, auto-properties, object initializers” 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 “Fields, auto-properties, object initializers”?

Define fields and auto-properties, and create objects with concise object initializers. 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 “Fields, auto-properties, object initializers” 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. Fields, auto-properties, object initializers
  2. Constructors, static members
  3. Encapsulation & invariants
← Back to C# Academy