Constructors help create objects with initial values. They set up things so your object is ready to use.
Constructors and initialization in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
class ClassName { // Constructor public ClassName(parameters) { // Initialization code } }
The constructor has the same name as the class.
It does not have a return type, not even void.
Examples
C Sharp (C#)
class Person { public string Name; public Person(string name) { Name = name; } }
C Sharp (C#)
class Car { public int Year; public Car() { Year = 2024; // default year } }
C Sharp (C#)
class Book { public string Title; public string Author; public Book(string title, string author) { Title = title; Author = author; } }
Sample Program
This program creates a Dog with a name and age using a constructor. Then it calls Bark to show the dog's info.
C Sharp (C#)
using System; class Dog { public string Name; public int Age; public Dog(string name, int age) { Name = name; Age = age; } public void Bark() { Console.WriteLine($"{Name} says: Woof! I am {Age} years old."); } } class Program { static void Main() { Dog myDog = new Dog("Buddy", 3); myDog.Bark(); } }
Important Notes
If you don't write a constructor, C# gives a default one with no parameters.
You can have many constructors with different parameters (called overloading).
Use constructors to avoid forgetting to set important data after creating objects.
Summary
Constructors set up new objects with starting values.
They have the same name as the class and no return type.
You can have multiple constructors with different inputs.
Practice
1. What is the main purpose of a constructor in a C# class?
easy
Solution
Step 1: Understand what constructors do
Constructors are special methods that run when an object is created to set initial values.Step 2: Compare options with constructor purpose
Only To initialize new objects with starting values describes initializing new objects, which matches the constructor's role.Final Answer:
To initialize new objects with starting values -> Option CQuick Check:
Constructor purpose = initialize objects [OK]
Hint: Constructors set initial values when creating objects [OK]
Common Mistakes:
- Confusing constructors with regular methods
- Thinking constructors return values
- Mixing constructors with inheritance
2. Which of the following is the correct syntax for a constructor in C# for a class named
Car?easy
Solution
Step 1: Recall constructor syntax rules
Constructors have the same name as the class and no return type, but must have an access modifier like public.Step 2: Check each option
public Car() { } matches: public + class name + parentheses + no return type. Others have void return or wrong syntax.Final Answer:
public Car() { } -> Option AQuick Check:
Constructor syntax = public ClassName() [OK]
Hint: Constructor name = class name, no return type [OK]
Common Mistakes:
- Adding a return type like void
- Using incorrect parameter syntax
- Omitting access modifier
3. What will be the output of the following C# code?
class Person {
public string Name;
public Person(string name) {
Name = name;
}
}
var p = new Person("Anna");
Console.WriteLine(p.Name);medium
Solution
Step 1: Understand constructor usage
The constructor sets the Name field to the passed string "Anna" when creating the Person object.Step 2: Check output of Console.WriteLine
Since p.Name was set to "Anna", printing p.Name outputs "Anna".Final Answer:
Anna -> Option BQuick Check:
Constructor sets Name = "Anna" so output = Anna [OK]
Hint: Constructor sets fields; output shows assigned value [OK]
Common Mistakes:
- Assuming default null value instead of assigned
- Confusing field name with value
- Expecting compilation error due to constructor
4. Identify the error in this C# class constructor and how to fix it:
class Book {
public string Title;
public Book(string title) {
title = Title;
}
}medium
Solution
Step 1: Analyze the assignment inside constructor
The code assigns title = Title, which sets the parameter to the field's value, not the other way around.Step 2: Correct the assignment direction
It should assign the field Title to the parameter value: Title = title; to initialize properly.Final Answer:
The assignment is reversed; should be Title = title; -> Option DQuick Check:
Field = parameter to initialize correctly [OK]
Hint: Assign field = parameter inside constructor [OK]
Common Mistakes:
- Reversing assignment direction
- Changing constructor name incorrectly
- Adding return type to constructor
5. Given this class with two constructors:
What is the output?
class Rectangle {
public int Width, Height;
public Rectangle() {
Width = 1;
Height = 1;
}
public Rectangle(int size) {
Width = size;
Height = size;
}
}
var r1 = new Rectangle();
var r2 = new Rectangle(5);
Console.WriteLine(r1.Width + "," + r1.Height);
Console.WriteLine(r2.Width + "," + r2.Height);What is the output?
hard
Solution
Step 1: Understand constructor overloading
The class has two constructors: one with no parameters sets Width and Height to 1; the other sets both to the given size.Step 2: Trace object creation and output
r1 uses the no-parameter constructor, so Width=1, Height=1. r2 uses the int parameter constructor with 5, so Width=5, Height=5.Final Answer:
1,1 5,5 -> Option AQuick Check:
Overloaded constructors set different sizes correctly [OK]
Hint: Overloaded constructors run based on arguments [OK]
Common Mistakes:
- Assuming default values are zero
- Thinking constructor overload causes error
- Mixing up which constructor runs
