C# > Object-Oriented Programming (OOP) > Classes and Objects > Object Instantiation
Basic Object Instantiation in C#
This code snippet demonstrates the fundamental concept of object instantiation in C# using a simple `Dog` class. It illustrates how to create objects (instances) from a class blueprint.
Defining the Class (Blueprint)
First, we define a class named `Dog`. This class acts as a blueprint for creating dog objects. It has two properties: `Name` (a string representing the dog's name) and `Breed` (a string representing the dog's breed). It also has a constructor that initializes these properties when a new `Dog` object is created, and a `Bark` method that simulates the dog barking.
public class Dog
{
public string Name { get; set; }
public string Breed { get; set; }
public Dog(string name, string breed)
{
Name = name;
Breed = breed;
}
public void Bark()
{
Console.WriteLine("Woof!");
}
}
Object Instantiation
In the `Main` method, we create two instances (objects) of the `Dog` class: `myDog` and `anotherDog`. The `new` keyword is used to allocate memory for each object, and the constructor (`Dog(string name, string breed)`) is called to initialize the object's properties with specific values. We then access the object's properties using the dot notation (`.`) and call the `Bark` method. Each object has its own independent set of properties.
public class Program
{
public static void Main(string[] args)
{
// Creating instances of the Dog class
Dog myDog = new Dog("Buddy", "Golden Retriever");
Dog anotherDog = new Dog("Lucy", "Poodle");
// Accessing object properties
Console.WriteLine($"{myDog.Name} is a {myDog.Breed}.");
Console.WriteLine($"{anotherDog.Name} is a {anotherDog.Breed}.");
// Calling a method on an object
myDog.Bark(); // Outputs: Woof!
anotherDog.Bark(); // Outputs: Woof!
}
}
Concepts Behind the Snippet
This snippet demonstrates the core principles of OOP:
Real-Life Use Case
Imagine building a game. You might have a `Player` class. Each player in the game would be an object instantiated from the `Player` class. Each player object would have its own unique properties (health, position, inventory) and methods (move, attack, defend).
Best Practices
Interview Tip
Be prepared to explain the difference between a class and an object. A class is a blueprint, while an object is a specific instance of that blueprint. Also, understand the role of constructors in object initialization.
When to Use Them
Use classes and objects whenever you need to model real-world entities or concepts in your code. They are fundamental to building well-structured and maintainable applications.
Memory Footprint
Each object instance consumes memory. The size of the memory footprint depends on the data types of the properties in the class. Creating a large number of objects can potentially impact memory usage, so it's important to manage object lifecycles effectively (e.g., using `Dispose` for objects that hold resources).
Alternatives
While OOP is a common approach, alternatives like Functional Programming exist. Instead of objects with state, you might use pure functions that operate on immutable data. Another approach is to use structs instead of classes in certain scenarios, structs are value types so they are stored on the stack.
Pros
Cons
FAQ
-
What is the difference between a class and an object?
A class is a blueprint or template for creating objects. An object is an instance of a class, meaning it's a concrete realization of the class with its own unique state (values for its properties). -
What is the purpose of a constructor?
A constructor is a special method that is called automatically when an object is created. Its purpose is to initialize the object's state (set initial values for its properties). -
What does the `new` keyword do?
The `new` keyword is used to allocate memory for a new object on the heap and then call the class's constructor to initialize the object.