How Do You Create a Class in C#?
๐ก Concept: Creating a Class in C#
In C#, a class is a blueprint for creating objects. It encapsulates data for the object and methods to manipulate that data. A class defines types, behavior, and structure that instances (objects) of that class will share.
๐ Quick Intro
Creating a class in C# involves using the class
keyword followed by the class name. A class can contain fields, properties, methods, constructors, and other members. It serves as the foundation for object-oriented programming in C#.
๐ง Analogy
Think of a class as a cookie cutter and the objects as cookies made from it. The cutter defines the shape (structure and behavior), while each cookie is an instance with its own unique values.
๐ง Technical Explanation
- โ
Use the
class
keyword followed by the name. - โ A class can include fields, constructors, methods, and properties.
- โ By default, class members are private unless specified otherwise.
- โ Classes support inheritance and polymorphism.
- โ C# also supports static classes which cannot be instantiated.
๐ฏ Use Cases
- ๐ฆ Encapsulate business logic for objects such as
Customer
,Order
, etc. - ๐ Organize code in an object-oriented manner.
- ๐ Provide reusable components with methods and properties.
- ๐ Support inheritance and polymorphism for extendable systems.
๐ป Code Example
// Creating a basic class
public class Car {
public string Make;
public string Model;
public void Drive() {
Console.WriteLine(""Driving the car..."");
}
}
// Using the class
Car myCar = new Car();
myCar.Make = ""Toyota"";
myCar.Model = ""Camry"";
myCar.Drive();

โ Interview Q&A
Q1: How do you declare a class in C#?
A: Using the class
keyword followed by the class name.
Q2: What can a class contain?
A: Fields, properties, methods, constructors, and events.
Q3: What is the default access modifier of class members?
A: Private.
Q4: Can a class be static in C#?
A: Yes, but it cannot be instantiated.
Q5: Can classes inherit from other classes?
A: Yes, C# supports single inheritance.
Q6: How do you create an object from a class?
A: Using the new
keyword.
Q7: What is encapsulation in the context of a class?
A: Hiding data using access modifiers and exposing it via properties.
Q8: Can a class contain other classes?
A: Yes, nested classes are allowed.
Q9: What is the difference between class and object?
A: A class is a blueprint; an object is an instance of the class.
Q10: Can we overload methods in a class?
A: Yes, method overloading is supported.
๐ MCQs
Q1. What keyword is used to define a class in C#?
- struct
- define
- class
- object
Q2. Which members can a class contain?
- Fields
- Methods
- Properties
- All of the above
Q3. What is the default access modifier of a class member?
- Public
- Protected
- Private
- Internal
Q4. Can a class be abstract?
- No
- Yes
- Only static
- Only sealed
Q5. What is instantiation?
- Defining class
- Importing class
- Deleting class
- Creating an object from a class
Q6. Which keyword is used to create an object?
- make
- this
- new
- instanceof
Q7. Can a class contain static members?
- No
- Yes
- Only methods
- Only constants
Q8. Which of the following is not part of a class?
- Constructor
- Method
- Namespace
- Property
Q9. What is a constructor used for?
- Delete class
- Initialize object
- Compile code
- None
Q10. What is true about a class?
- It's a variable
- It's a method
- It is a blueprint for objects
- None
๐ก Bonus Insight
Good class design follows the SOLID principles. Keep class responsibilities narrow, use access modifiers wisely, and rely on composition over inheritance wherever possible.
๐ PDF Download
Need a handy summary for your notes? Download this topic as a PDF!