Class with Example

What is a class ? Support your answers with proper elaboration and example.

A class, in simple terms, is a blueprint of an object which defines all the common properties of one or more objects that are associated with it. A class can be used to define multiple objects within a program. Class is an user-defined data type. To create a class, you start with the class keyword followed by a name and its body delimited by curly brackets.

For example

class MyClass

{

  // Class members

  string color = "red";        // field

  int maxSpeed = 200;          // field

  public void fullThrottle()   // method

  {

    Console.WriteLine("The car is going as fast as it can!");

  }

}

Leave Comment

Important Topics

Title
Object Oriented Programming (OOP)
OOP vs POP
Class with Example
Objects
Access Modifiers
Encapsulation with example
C# Properties (GET, SET)
Method and Function
Abstraction
Polymorphism
Operator Overloading
Inheritance
Constructor and its Types
Exception Handling
Throwing an Exception