C# – Classes and Objects

we will start object oriented programming (OOP) in C#. We will start with learning classes , objects and their basics. Then we will move to constructors, access modifiers, properties, method overloading and static methods.

Concepts of Class

A class is simply an abstract model used to define a new data types. A Class may contain any combination of encapsulated data ( fields or member variables), operations that can be performed on data (methods) and accessors to data (properties).


class MyClass


// fields, operations and properties go here


where My Class is the name of class or new data type that we are defining here.


An object is an instance of a class. it has its own state, behavior and identity.

MyClass myobjectReference = new Myclass ();

Student Class
String studentName
int studentRollNum

Student Object1 Student Object2 Student Object3

StudentName = “abc” StudentName = “pqr” StudentName = “xyz”

StudentRollNum = 1 StudentRollNum = 2 StudentRollNum = 3


Fields are the data contained in the class. Fields may be implicit data types, objects of some other class.

class student


// fields contained in student class

string name;

int age;

int marksInMaths;

int marksInEnglish;

int marksInScience;

int totalMarks = 300; // initialization

int ObtainedMarks;

double Percentage;

You can also initialize the fields with the initial values as we did in totalMarks in the example above. If you don’t initialize the members of the class, they will be initialized with their default values.

Default values for different data types are shown below:

Data TypeDefault Value
char‘\0’ (null character)
string“” (empty string)


Methods are the operations performed on the data. A method may take some input values through its parameters and may return a value of a particular data type.

To use a method, you need to –

  1. Define the method
  2. call the method

Defining Methods in C#

When you define a method, you basically declare the elements of its structure. The syntax for defining a method in C# is as follows –

<Access Specifier> < Return Type> <Method Body>


Following are the various elements of a method –

  1. Access Specifier – This determines the visibility of a variable or a method from another class.
  2. Return Type – A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is Void.
  3. Method Name – Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.
  4. Parameter List – Enclosed between parentheses , the parameters are used to pass and receive data from a method. The parameter list refers to the type, order and method. Parameters are optional; that is a method may contain no parameters.
  5. Method body – This contains the set of instructions needed to complete the required activity.

<return type> < name of methods> (<data type><identifier>, <data type> <identifier>,….)


// body of the method


For Example,

int FindSum ( int num1, int num2)


int sum = num1 + num2;

return sum;


Calling Methods in C#

you can call a method using the name of the method. The following example illustrates this –

using system;

namespace calculatorApplication {

class NumberMainpulator {

public int FindMax (int num1, int num2)

/* local variable declaration */

int result;

if (num1 > num2)

result = num1;


result = num2

return result;


static void Main ( string []args) {

/* local variable definition */

int a = 100;

int b = 200;

int ret;

NumberManipulator n = new NumberManipulator();

// calling the FindMax method

ret = n. FindMax ( a, b );

Console.WriteLine (” Max value is : {0}”, ret );

Console.ReadLine ( );




When the above code is compiled and executed , it produces the following result –

Max Value is : 200

Leave a Reply

Your email address will not be published. Required fields are marked *