C# > Object-Oriented Programming (OOP) > Classes and Objects > Class Declaration
Basic Class Declaration in C#
This snippet demonstrates a fundamental class declaration in C#. It showcases the basic structure of a class, including its name and member variables (fields) and methods.
Basic Class Structure
This code defines a class named `Car`. It has three public fields: `Make`, `Model`, and `Year`, representing the car's manufacturer, model name, and manufacturing year respectively. A constructor is defined to initialize these fields when a new `Car` object is created. The `DisplayCarDetails` method prints the car's information to the console.
public class Car
{
// Fields (Member Variables)
public string Make;
public string Model;
public int Year;
// Constructor
public Car(string make, string model, int year)
{
Make = make;
Model = model;
Year = year;
}
// Method
public void DisplayCarDetails()
{
Console.WriteLine($"Make: {Make}, Model: {Model}, Year: {Year}");
}
}
Concepts Behind the Snippet
Class Declaration: The class
keyword is used to define a new class. The class acts as a blueprint for creating objects.
Fields: These are variables that hold data related to the class. In this case, `Make`, `Model`, and `Year` store the car's specific details.
Constructor: A special method that is automatically called when a new object of the class is created. It is used to initialize the object's state.
Methods: Functions defined within a class that perform actions. `DisplayCarDetails` is a method that displays the car's details.
Real-Life Use Case
Imagine building a software system for a car dealership. You could use the `Car` class to represent each car in the dealership's inventory. Each `Car` object would store information like make, model, year, price, and features. Methods could be added to calculate the car's value or generate a sales report.
Best Practices
public
, private
, protected
, internal
) for your class members. Use the most restrictive access modifier that still allows the necessary functionality. In this case, public
is used for simplicity, but in a real-world application, you might use properties with backing fields to control access to the data.
Interview Tip
Be prepared to explain the difference between a class and an object. A class is a blueprint or template, while an object is an instance of that class. Also, be ready to discuss the four pillars of OOP: encapsulation, inheritance, polymorphism, and abstraction.
When to Use Classes
Use classes when you need to model real-world entities or concepts. Classes allow you to group related data (fields) and behavior (methods) into a single, reusable unit. They are fundamental for organizing and structuring complex software systems. Classes are useful for creating reusable components that can be easily maintained and extended.
Memory Footprint
The memory footprint of a class depends on the size of its fields. Each object of the class will consume memory based on the data types of its fields. Classes with many fields or fields of large data types (e.g., large strings, arrays) will have a larger memory footprint. It's crucial to design your classes with efficient data structures to optimize memory usage, especially when dealing with a large number of objects.
Alternatives
Pros
Cons
FAQ
-
What is the difference between a class and an object?
A class is a blueprint or template that defines the characteristics (fields) and behaviors (methods) of an object. An object is an instance of a class, representing a specific entity with its own unique data values. -
What is a constructor?
A constructor is a special method that is automatically called when a new object of a class is created. It is used to initialize the object's state by setting the initial values of its fields. Constructors have the same name as the class. -
What are access modifiers?
Access modifiers control the visibility and accessibility of class members (fields, methods, etc.). Common access modifiers include `public`, `private`, `protected`, and `internal`. `public` members are accessible from anywhere, `private` members are only accessible within the class, `protected` members are accessible within the class and its derived classes, and `internal` members are accessible within the same assembly.