YouTip LogoYouTip

Csharp Class

When you define a class, you define a blueprint for a data type. This doesn't actually define any data itself, but it defines what the class name means. That is, what an object of the class consists of and what operations can be performed on such an object. Objects are instances of a class. The methods and variables that constitute a class are called members of the class. A class definition starts with the keyword **class** followed by the class name. The body of the class is enclosed between a pair of curly braces. Here is the general form of a class definition: class class_name { // member variables variable1; variable2; ... variableN; // member methods method1(parameter_list) { // method body } method2(parameter_list) { // method body } ... methodN(parameter_list) { // method body } } Please note: * The access specifier determines the access rules for the class as well as the class members. If not specified, the default access identifier is **internal** for the class and **private** for the members. * The data type specifies the type of variable, and the return type specifies the data type returned by the method. * To access a member of a class, you use the dot (.) operator. * The dot operator links the object's name with the member's name. The following example illustrates the concepts discussed so far: ## Example using System; namespace BoxApplication { class Box { public double length;// Length public double breadth;// Width public double height;// Height } class Boxtester { static void Main(string[] args) { Box Box1 =new Box();// Declare Box1 of type Box Box Box2 =new Box();// Declare Box2 of type Box double volume =0.0;// Volume // Box 1 details Box1.height=5.0; Box1.length=6.0; Box1.breadth=7.0; // Box 2 details Box2.height=10.0; Box2.length=12.0; Box2.breadth=13.0; // volume of box 1 volume = Box1.height* Box1.length* Box1.breadth; Console.WriteLine("Volume of Box1: {0}", volume); // volume of box 2 volume = Box2.height* Box2.length* Box2.breadth; Console.WriteLine("Volume of Box2: {0}", volume); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Volume of Box1: 210Volume of Box2: 1560 A member function of a class is a function that has its definition or its prototype within the class definition just like any other variable. As a member of a class, it can operate on any object of that class and can access all the members of a class. Member variables are attributes (from a design point of view) and they are kept private to implement encapsulation. These variables can only be accessed using public member functions. Let us set and get the different class member values using the above concepts: ## Example using System; namespace BoxApplication { class Box { private double length;// Length private double breadth;// Width private double height;// Height public void setLength(double len ) { length = len; } public void setBreadth(double bre ) { breadth = bre; } public void setHeight(double hei ) { height = hei; } public double getVolume() { return length * breadth * height; } } class Boxtester { static void Main(string[] args) { Box Box1 =new Box();// Declare Box1 of type Box Box Box2 =new Box();// Declare Box2 of type Box double volume;// Volume // Box 1 details Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box 2 details Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // volume of box 1 volume = Box1.getVolume(); Console.WriteLine("Volume of Box1: {0}" ,volume); // volume of box 2 volume = Box2.getVolume(); Console.WriteLine("Volume of Box2: {0}", volume); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Volume of Box1: 210Volume of Box2: 1560 A **constructor** is a special member function of a class which is executed whenever a new instance of the class is created. The constructor has the same name as the class and it does not have any return type. The following example illustrates the concept of a constructor: ## Example using System; namespace LineApplication { class Line { private double length;// Length of a line public Line() { Console.WriteLine("Object created"); } public void setLength(double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line =new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line: {0}", line.getLength()); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Object createdLength of line: 6 A **default constructor** does not have any parameter. But if you need a constructor that has parameters, you can create a **parameterized constructor**. This technique helps you to assign initial value to an object at the time of creation. See the example below for a parameterized constructor: ## Example using System; namespace LineApplication { class Line { private double length;// Length of a line public Line(double len)// Parameterized constructor { Console.WriteLine("Object created, length = {0}", len); length = len; } public void setLength(double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line =new Line(10.0); Console.WriteLine("Length of line: {0}", line.getLength()); // set line length line.setLength(6.0); Console.WriteLine("Length of line: {0}", line.getLength()); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Object created, length = 10Length of line: 10Length of line: 6 A **destructor** is a special member function of a class which is executed when an object of its class goes out of scope. A destructor has the same name as the class, prefixed with a tilde (~), it does not take any parameter and does not return anything. A destructor is used to release resources (for example, closing files, releasing memory, etc.) before the end of the program. A destructor cannot be inherited or overloaded. The following example illustrates the concept of a destructor: ## Example using System; namespace LineApplication { class Line { private double length;// Length of a line public Line()// Constructor { Console.WriteLine("Object created"); } ~Line()//destructor { Console.WriteLine("Object deleted"); } public void setLength(double len ) { length = len; } public double getLength() { return length; } static void Main(string[] args) { Line line =new Line(); // set line length line.setLength(6.0); Console.WriteLine("Length of line: {0}", line.getLength()); } } } When the above code is compiled and executed, it produces the following result: Object createdLength of line: 6Object deleted We can define class members as static using the **static** keyword. When we declare a member of a class as static, it means no matter how many objects of the class are created, there is only one copy of the static member. The keyword **static** means that the member belongs to the class itself rather than to a specific instance. Static variables are used to define constants because their values can be retrieved by directly calling the class without needing to create an instance of the class. Static variables can be initialized outside of a member function or class definition. You can also initialize a static variable inside the class definition. The following example demonstrates the use of a **static variable**: ## Example using System; namespace StaticVarApplication { class StaticVar { public static int num; public void count() { num++; } public int getNum() { return num; } } class StaticTester { static void Main(string[] args) { StaticVar s1 =new StaticVar(); StaticVar s2 =new StaticVar(); s1.count(); s1.count(); s1.count(); s2.count(); s2.count(); s2.count(); Console.WriteLine("s1 's Variable num: {0}", s1.getNum()); Console.WriteLine("s2 's Variable num: {0}", s2.getNum()); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: s1 's Variable num: 6 s2 's Variable num: 6 You can also declare a **member function** as **static**. Such functions can only access static variables. Static functions exist even before an object is created. The following example demonstrates the use of a **static function**: ## Example using System; namespace StaticVarApplication { class StaticVar { public static int num; public void count() { num++; } public static int getNum() { return num; } } class StaticTester { static void Main(string[] args) { StaticVar s =new StaticVar(); s.count(); s.count(); s.count(); Console.WriteLine("Variable num: {0}", StaticVar.getNum()); Console.ReadKey(); } } } When the above code is compiled and executed, it produces the following result: Variable num: 3
← Csharp InheritanceSel Invalid β†’