Ticker

6/recent/ticker-posts

Class and object in C#

 Class and object


A class is a construct that enables us to create your own custom types by grouping together variables of other types, methods, and events. Together, these elements are called class members. A class is simply an abstract model used to define 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).

An object is the concrete realization of an instance built on the model specified by the class. An object is created in the memory using the keyword ‗new‘ and is referenced by an identifier called a ―reference‖.

For example: MyClass myClassObject=new MyClass();

In this example, an object of MyClass is referenced by an identifier myClassObject .

Fields are the data contained in the class. Fields maybe implicit data types, objects of some other class, enumerations, structs or delegates

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

A class the definition starts with the keyword class followed by the class name; and the class body enclosed by a pair of curly braces. Following is the general form of a class definition

<access specifier> class class_name

{

// member variables

<access specifier> <data type> variable1;

<access specifier> <data type> variable2;

...

<access specifier> <data type> variableN;

// member methods

<access specifier> <return type> method1(parameter_list)

{

// method body

}

<access specifier> <return type> method2(parameter_list)

{

// method body

}

...

<access specifier> <return type> methodN(parameter_list)

{

// method body

}

}

Example:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace Unit_1

{

class Box {

double width;

double height;

double depth;

// compute and return volume

double volume() {

return width * height * depth;

}

// sets dimensions of box

void setDim(double w, double h, double d) {

width = w;

height = h;

depth = d;

}

}

class BoxDemo {

public static void main(String [] args) {

Box mybox1 = new Box();

Box mybox2 = new Box();

double vol;

 

// initialize each box

mybox1.setDim(10, 20, 15);

mybox2.setDim(3, 6, 9);

 

// get volume of first box

vol = mybox1.volume();

Console.WriteLine("Volume is " + vol);

 

// get volume of second box

vol = mybox2.volume();

Console.WriteLine("Volume is " + vol);

}

}

}

A class can be static or non-static.

Static Class and Static Members

A static class is a class that cannot be instantiated. Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity i.e. the data and functions do not change regardless of what happens to the object.

The main features of a static class are:

 They only contain static members.

 They cannot be instantiated.

 They are sealed.

 They cannot contain instance constructors but can have a static constructor

Keyword static is used to define a static class and static class members.

Example

static class CompanyInfo

{

public static string GetCompanyName() { return "CompanyName"; }

public static string GetCompanyAddress() { return "CompanyAddress"; }

//...

}

Post a Comment

0 Comments