Home To Know Codes C# Class

C# Class

321
0
SHARE
C# Classes

A class definition starts with the keyword class followed by the class name and the class body enclosed by pair of curly braces. It is a group of related method and variable. Class is a like a blueprint it defines the data and behavior of type. You create an instance of this class now referred to as an object. You can create as many instance of your class as you want to.

Declaring Class

Declaring Class Example Input
Declaring Class Example Input
  1. Access specifiers specify the access rules for the members as well as the class itself. By default access specifier for the class type is internal and private for class member.
  2. To access the class member you will use the object with dot (.) operator.

Create Object

A class defines a type of object. An object is a concrete entity based on a class and is sometimes referred to as an instance of a class. Object can be created by using the new keyword. new keyword is used to
occupy the memory space for object.

Create Object Example Input
Create Object Example Input

Every class has a default constructor. A class constructor is a special member function of a class that is executed whenever we create new objects of that class.

Member Function and Variable

A member function of a class is a function that has its definition or its prototype within the class definition like any other variable. The member of a class is operated by the object. We can also define class members as static using the static keyword. Such functions can access only static variables. The static functions exist even before the object is created.

Member Function Example Input
Member Function Example Input

Member Function Example Output
Member Function Example Output

Leave a Reply