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!

๐Ÿ” Navigation

Learn More About C# ๐Ÿ“š

1. What is C#? ๐Ÿ‘‰ Explained
2. Main Features of C# ๐Ÿ‘‰ Explained
3. Difference Between C# and Java ๐Ÿ‘‰ Explained
4. Common Language Runtime (CLR) in C# ๐Ÿ‘‰ Explained
5. Common Type System (CTS) in C# ๐Ÿ‘‰ Explained
6. Common Language Specification (CLS) in C# ๐Ÿ‘‰ Explained
7. Value Types vs Reference Types in C# ๐Ÿ‘‰ Explained
8. What is a Namespace in C#? ๐Ÿ‘‰ Explained
9. Purpose of the 'using' Keyword in C# ๐Ÿ‘‰ Explained
10. Different Data Types in C# ๐Ÿ‘‰ Explained
11. Difference Between int and Int32 in C# ๐Ÿ‘‰ Explained
12. Difference Between float, double, and decimal in C# ๐Ÿ‘‰ Explained
13. What is the Default Value of a Boolean in C#? ๐Ÿ‘‰ Explained
14. What is Boxing and Unboxing in C# ๐Ÿ‘‰ Explained
15. What are the Different Types of Operators in C# ๐Ÿ‘‰ Explained
16. Difference Between Equals and == in C# ๐Ÿ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# ๐Ÿ‘‰ Explained
18. What is the Ternary Operator in C# ๐Ÿ‘‰ Explained
19. How Does the Switch Statement Work in C# ๐Ÿ‘‰ Explained
20. What is Object-Oriented Programming in C# ๐Ÿ‘‰ Explained
21. What are the Four Pillars of OOP in C# ๐Ÿ‘‰ Explained
22. What is Encapsulation in C# ๐Ÿ‘‰ Explained
23. What is Inheritance in C# ๐Ÿ‘‰ Explained
24. What is Polymorphism in C# ๐Ÿ‘‰ Explained
25. What is Abstraction in C# ๐Ÿ‘‰ Explained
26. What is an Abstract Class in C# ๐Ÿ‘‰ Explained
27. What is an Interface in C# ๐Ÿ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? ๐Ÿ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# ๐Ÿ‘‰ Explained
30. How Do You Create a Class in C#? ๐Ÿ‘‰ Explained
31. What is a Constructor in C# ๐Ÿ‘‰ Explained
32. What Are the Types of Constructors in C# ๐Ÿ‘‰ Explained
33. What is a Static Constructor in C# ๐Ÿ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# ๐Ÿ‘‰ Explained
35. What is the Use of 'this' Keyword in C# ๐Ÿ‘‰ Explained
36. What is a Destructor in C# ๐Ÿ‘‰ Explained
37. What is Object Initializer Syntax in C# ๐Ÿ‘‰ Explained
38. What is the Difference Between Field and Property in C# ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup