Home » C#.Net » Introduction to Class in OOPS with C#.net

Introduction to Class in OOPS with C#.net

Understanding class

A class is like a blueprint. It allows us to create our own custom data types by grouping methods, events and variables of different types like int, string or any other custom types. Class defines the data and behavior of a type.

A class can be defined with a keyword “class” followed by the class name and body of the class with a pair of curly braces.
Let’s have a look at the general form of a class body.

below is the general form of a class body.

//Public is the access specifier while Calculator is the name of the class
public class Calculator
//variables declaration
private int var1;
private int var2;

//methods declaration
public decimal Add(decimal val1, decimal val2)
return val1+val2;

Classes supports inheritance while structs can’t support it.
Access specifiers plays an important role in the class definition, which specify the rules for accessing the member as well as the class itself. Default access specifier for the class is internal while for member it is private.

Data type specifies the type of variable, and return type specifies the data type of the data the method returns.

To access the class members we need to create an object of the class if class is not static, and using object name(dot)membername or member function name

See the below code for accessing Add method of Calculator class

Calculator calc = new Calculator();
decimal finalSum = calc.Add(10,20);



Site Admin is a writer of http://www.codingsharp.com website. He is a software professional with hands on 4+ years of experience. Generally he writes an article about Javascript, SQL Server and JQuery. Here he shares his notes, experiences, examples.

About Coding Sharp

Coding sharp is a blog where you can find the authors personal experience, tutorials, examples, code samples, resources related to C#, Asp.Net, Linq, Ajax, MVC, Entity Framework, SQL Server, jQuery, Visual Studio.