C# type
When you define a class, you define a blueprint for a data type. This does not actually define any data, but it defines what the name of the class means, that is, what the object of the class consists of and what operations can be performed on this object. An object is an instance of a class. The methods and variables that make up a class are called members of the class.
Class definition
The definition of a class starts with the keyword class, followed by the name of the class. Class, contained in a pair of curly braces. The following is the general form of 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 } }
- The access identifier specifies the access rules for the class and its members. If not specified, the default access identifier is used. The default access identifier of a class is internal, and the default access identifier of a member is private.
- The data type specifies the type of the variable, and the return type specifies the data type returned by the returned method.
- If you want to access members of a class, you use the dot (.) operator.
- The dot operator links the name of the object and the name of the member.
The following examples illustrate 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, type Box Box Box2 = new Box(); // Declare Box2, type Box double volume = 0.0; // volume // Box1 details Box1.height = 5.0; Box1.length = 6.0; Box1.breadth = 7.0; // Box2 details Box2.height = 10.0; Box2.length = 12.0; Box2.breadth = 13.0; // Volume of Box1 volume = Box1.height * Box1.length * Box1.breadth; Console.WriteLine("Box1 Volume of: {0}", volume); // Volume of Box2 volume = Box2.height * Box2.length * Box2.breadth; Console.WriteLine("Box2 Volume of: {0}", volume); Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce the following results:
Volume of Box1: 210
Box2 volume: 1560
Member functions and encapsulation
A member function of a class is a function with its definition or prototype in the class definition, just like other variables. As a member of a class, it can operate on any object of the class and access all members of the class of the object.
Member variables are properties of objects (from a design perspective) and they remain private to implement encapsulation. These variables can only be accessed using public member functions.
Let's use the above concepts to set and obtain the values of different class members in a class:
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, type Box Box Box2 = new Box(); // Declare Box2, type Box double volume; // volume // Box1 details Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // Box2 details Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // Volume of Box1 volume = Box1.getVolume(); Console.WriteLine("Box1 Volume of: {0}" ,volume); // Volume of Box2 volume = Box2.getVolume(); Console.WriteLine("Box2 Volume of: {0}", volume); Console.ReadKey(); } } }
Constructor in C #
The constructor of a class is a special member function of the class, which is executed when a new object of the class is created.
The constructor has exactly the same name as the class, and it has no return type.
The following example illustrates the concept of constructor:
using System; namespace LineApplication { class Line { private double length; // Length of 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(); } } }