One can create new reference types by defining classes. Classes provide templates’ from which these direct instances are generated. Where we appeal to the relation between a class and its corresponding reference type instances we shall say that a class specifies the type (also that the class specifies the constitutive elements of the type)
Any type is made up of elements, which we term type members. There are two main kinds of type members that a class can specify. Firstly, a class can specify other types- both value and reference. This idea, that types can contain other types, is known within the literature on object orientation as ‘containment’, or else ‘aggregation’. Where a type contains another reference type, we shall call it the containing type of the latter.
The second, main kind· of type members that a class can specify are methods, functions designed for reading and manipulating the value and reference types an instance contains.
Objects in C# are created from types, just like a variable. This type of an object is known as class. we can use class definition to instantiate objects, which means to create a real named instance of a class.
We’ll be covering the following topics in this tutorial:
Declaration of Classes
Class is an user-defined data type. To create a class, you start with the class keyword followed by a name and its body delimited by curly brackets. The following is an example of a very simple class declaration
class classname
{
// class-body
}
The class-body contains the member data and member function of the class. C++ programmer must note that there is no semicolon after the closing brace of the class.
Members of Class
Class is a mechanism to implement the encapsulation, it bind the data and a function in a single unit. Therefore data and functions are the members of class, which is known as member data and member function.
There is an example of full class:
class circle
{
double radius;
public void get_radius(double r)
{
radius = r;
}
public double area()
{
return ( 3.14 * r * r);
}
}
Member Access Modifiers
Access modifiers provide the accessibility control for the members of classes to outside the class. They also provide the concept of data hiding. There are five member access modifiers provided by the C# Language.
By default all members of class have private accessibility. If we want a member to have any other accessibility, then we must specify a suitable access modifier to it individually.
For Example:
class Demo
{
public int a;
interna1 int x;
protected double d;
float m; // private by default
}
We cannot declare more than one member data or member function under an accessibility modifier.
Read Only Member
When creating a member variable of a class, one of the decisions you make consists of deciding how the field would get its value(s). Sometimes you will allow the clients of the class to change the values of the field. In some other cases, you may want the field to only hold or present the value without being able to change it. This can still allow the clients to access the field and its value but on a read-only basis.
To create a field whose value can only be read, precede its data type, during declaration, – with the readonly keyword. Here is an example:
Public readonly double PI;
After declaring the variable, you should initialize it. You have two main alternatives. You can initialize the field when declaring it. Here is an example:
The use of read only members.
using System;
namespace Geometry
{
class circle
{
public double Radius;
public circle(double rad)
{
Radius = rad;
}
Public readonly double pi = 3.14159
}
class Exercise
{
static int Main()
{
Circle circ = new Circle(24.72);
Console.writeline(“circle characteristics”);
Console.writeline(“radius: {0}”, circ.radius);
Console.writeline(“pi: {0}\n”,circ.pi);
Return 0;
}
}
}
OUTPUT:
circle Characteristics
Radius: 24.72
PI: 3.14159
Object creation
In C# objects are created using the new keyword. Actually new is an operator, creates an object of the specified class and returns a reference to that object. Here is an example :
public class Exercise
{
public void welcome()
{
Console.writeLine(“This is Exercise class”);
}
}
public class classl
{
static void Main()
{
Exercise exo = new Exercise ();
}