Constructors, static members
Build objects with constructors (including overloads and chaining) and use static fields/methods to hold data shared by all instances.
Constructors, static members is a free C# Academy lesson on CoddyKit — lesson 2 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.
Constructors & static: overview
Goal: Construct objects safely and share data across instances.
- Constructors set required state
- Overloads & this(...) chaining
- Static fields/methods shared by all objects
Basic constructor
A constructor runs when you call new and initializes the object.
using System;
// Constructor sets required data on creation
public class User
{
public string Name { get; private set; }
public int Age { get; private set; }
public User(string name, int age)
{
Name = name;
Age = age;
}
}
public class Program
{
public static void Main(string[] args)
{
User u = new User("Ada", 28);
Console.WriteLine(u.Name + " (" + u.Age + ")");
}
}
Overloads & chaining
Use constructor overloading for convenience and this(...) to reuse logic.
using System;
// Overloads let you offer defaults; chain with this(...)
public class Point2D
{
public int X { get; private set; }
public int Y { get; private set; }
public Point2D(int x, int y)
{
X = x; Y = y;
}
public Point2D(int xy) : this(xy, xy) { } // calls the main constructor
}
public class Program
{
public static void Main(string[] args)
{
Point2D p1 = new Point2D(3, 4);
Point2D p2 = new Point2D(5); // becomes (5,5)
Console.WriteLine("p1: " + p1.X + "," + p1.Y);
Console.WriteLine("p2: " + p2.X + "," + p2.Y);
}
}
Static members
Static members are shared by all instances. Call static methods via the type name.
using System;
// Static members live on the type (one copy shared)
public class Counter
{
private static int _created = 0; // shared count
public int Id { get; private set; }
public Counter()
{
_created = _created + 1;
Id = _created;
}
public static int CreatedCount()
{
return _created;
}
}
public class Program
{
public static void Main(string[] args)
{
Counter a = new Counter();
Counter b = new Counter();
Console.WriteLine("a.Id=" + a.Id + ", b.Id=" + b.Id);
Console.WriteLine("Total created = " + Counter.CreatedCount());
}
}
Static constructor & constants
A static constructor runs once per type. Use const for compile-time literals and static readonly for values set at runtime.
using System;
public class AppConfig
{
public const string AppName = "Demo"; // compile-time constant
public static readonly DateTime StartedAt; // runtime-initialized once
// Static constructor: runs once before first use of the type
static AppConfig()
{
StartedAt = DateTime.UtcNow;
}
public static string Info()
{
return AppName + " started at " + StartedAt.ToString("u");
}
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(AppConfig.Info());
Console.WriteLine(AppConfig.Info()); // static ctor ran only once
}
}
Tips
Tips:
- Keep constructors small; validate inputs early.
- Use overloads for common defaults; chain to a single main constructor.
- Use static when data truly belongs to the type (e.g., counters, helpers).
Static members concept
Recap
Recap: Use constructors to set required state, overload and chain for convenience, and add static members for data or helpers shared by all objects.
Frequently asked questions
Is the “Constructors, static members” lesson free?
Yes — the full text of “Constructors, static members” 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 “Constructors, static members”?
Build objects with constructors (including overloads and chaining) and use static fields/methods to hold data shared by all instances. 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 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Constructors, static members” 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
- Fields, auto-properties, object initializers
- Constructors, static members
- Encapsulation & invariants