Class (Class) in C #

When you define a class in C #, you define a blueprint for a data type. This does not really define any data, but it defines the meaning of that class name. That is, what an object of that class consists of, what activities can be performed on that object.

When you define a class in C #, you define a blueprint for a data type. This does not really define any data, but it defines the meaning of that class name. That is, what an object of that class consists of, what activities can be performed on that object. Objects are instances of a class. Methods and variables that make up a class are called members of that class.

Defining a Class in C #

A C # class definition starts with the class keyword followed by the class name; and the class body is surrounded by braces. Here is the general form of a class definition in C #:

  specifier > class tên_lớp { // cac bien thanh vien  specifier >  ể u_d ữ_ li ệ u > biến1;  specifier >  ể u_d ữ_ li ệ u > biến2; .  specifier >  ể u_d ữ_ li ệ u > biếnN; // cac phuong thuc thanh vien  specifier >  ể u_tr ả_ v ề > tên_phương_thức1(danh_sách_tham_số) { // phan than phuong thuc }  specifier >  ể u_tr ả_ v ề > tên_phương_thức2(danh_sách_tham_số) { // phan than phuong thuc } .  specifier >  ể u_tr ả_ v ề > tên_phương_thứcN(danh_sách_tham_số) { // phan than phuong thuc } } 

Note:

The access specifier defines access rules for members as well as the class itself. If not mentioned, Access Specifier default for a class type is internal . The default access mode for members is private .

Data_type defines the type of variable, and returns the data type that the method returns.

To access class members, you use the dot (.) Operator.

The dot (.) Operator is associated with the name of an object with the name of a member.

The following example illustrates the concepts of class in C # mentioned above: creating two classes named Box and TestCsharp in two separate files respectively.

Box class: contains the properties of a box

 using System ; namespace QTMCsharp { class Box { public double chieu_dai ; public double chieu_rong ; public double chieu_cao ; } } 

TestCsharp class: contains main () method to manipulate on Box object

 using System ; namespace QTMCsharp { public class TestCsharp { public static void Main ( string [] args ) { Console . WriteLine ( "Class trong C#" ); Console . WriteLine ( "------------------------n" ); Box Box1 = new Box (); // tao doi tuong Box1 Box Box2 = new Box (); // tao doi tuong Box2 double the_tich = 0.0 ; // the tich cua box // thong tin cua box1 Box1 . chieu_cao = 5.0 ; Box1 . chieu_dai = 6.0 ; Box1 . chieu_rong = 7.0 ; // thong tin cua box2 Box2 . chieu_cao = 10.0 ; Box2 . chieu_dai = 12.0 ; Box2 . chieu_rong = 13.0 ; // Tinh va in the tich cua box1 the_tich = Box1 . chieu_cao * Box1 . chieu_dai * Box1 . chieu_rong ; Console . WriteLine ( "The tich cua Box1 la: {0}" , the_tich ); // Tinh va in the tich cua box2 the_tich = Box2 . chieu_cao * Box2 . chieu_dai * Box2 . chieu_rong ; Console . WriteLine ( "The tich cua Box2 la: {0}" , the_tich ); Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

Picture 1 of Class (Class) in C #

Member functions and encapsulation in C #

A member function in C # of a class is a function that has its definition and prototype within the same class definition as any other variable. It works on any object of the class that is a member and has access to all members of a class for that object.

Member variables are attributes of an object (from the design context) and they are kept private to implement encapsulation. These variables can only be accessed using public member functions.

The following example illustrates how to use the above concepts to set and retrieve the values ​​of different members in a class: create two classes named Box and TestCsharp respectively in two separate files.

Class Box : contains member properties and methods

 using System ; namespace QTMCsharp { class Box { private double chieu_dai ; private double chieu_rong ; private double chieu_cao ; public void setChieuDai ( double len ) { chieu_dai = len ; } public void setChieuRong ( double bre ) { chieu_rong = bre ; } public void setChieuCao ( double hei ) { chieu_cao = hei ; } public double tinhTheTich () { return chieu_dai * chieu_rong * chieu_cao ; } } } 

TestCsharp class: contains main () method to manipulate on Box object

 using System ; namespace QTMCsharp { public class TestCsharp { public static void Main ( string [] args ) { Console . WriteLine ( "Class trong C#" ); Console . WriteLine ( "------------------------n" ); Box Box1 = new Box (); // tao doi tuong Box1 Box Box2 = new Box (); // tao doi tuong Box2 double the_tich ; // nhap thong tin cho Box1 Box1 . setChieuDai ( 6.0 ); Box1 . setChieuRong ( 7.0 ); Box1 . setChieuCao ( 5.0 ); // nhap thong tin cho Box2 Box2 . setChieuDai ( 12.0 ); Box2 . setChieuRong ( 13.0 ); Box2 . setChieuCao ( 10.0 ); // tinh va in the tich Box1 the_tich = Box1 . tinhTheTich (); Console . WriteLine ( "The tich Box1 la: {0}" , the_tich ); // tinh va in the tich Box2 the_tich = Box2 . tinhTheTich (); Console . WriteLine ( "The tich Box2 la: {0}" , the_tich ); Console . ReadKey (); } } } 

If you do not use the Console.ReadKey () command; then the program will run and finish (so fast that you can not see the results). This command allows us to see the results more clearly.

Compiling and running the above C # program will produce the following results:

Picture 2 of Class (Class) in C #

Constructor in C #

A constructor of a Class, which is a special member function of a class, is executed whenever we create new objects of that class.

A constructor has the same name as the class name and it doesn't have any return type. Here is an example to illustrate the concept of constructor in C #:

 using System ; namespace QTM Csharp { class Line { private double chieu_dai ; public Line () { Console . WriteLine ( "Doi tuong dang duoc tao" ); } public void setChieuDai ( double len ) { chieu_dai = len ; } public double getChieuDai () { return chieu_dai ; } static void Main ( string [] args ) { Console . WriteLine ( "Constructor trong C#" ); Console . WriteLine ( "---------------------" ); //tao doi tuong Line bang constructor Line line = new Line (); // thiet lap chieu dai cho duong line . setChieuDai ( 6.0 ); Console . WriteLine ( "Chieu dai cua duong la: {0}" , line . getChieuDai ()); Console . ReadKey (); } } } 

Compiling and running the above C # program will produce the following results:

Picture 3 of Class (Class) in C #

A default constructor in C # does not have any parameters, but if you need it, a constructor can have parameters. These constructors are called constructors that are parameterized . This technique helps you assign an initial value to an object at the time of its creation, as in the following example:

 using System ; namespace QTMCsharp { class Line { private double chieu_dai ; public Line ( double len ) //constructor co tham so { Console . WriteLine ( "Doi tuong dang duoc tao, chieu dai = {0}" , len ); chieu_dai = len ; } public void setChieuDai ( double len ) { chieu_dai = len ; } public double getChieuDai () { return chieu_dai ; } static void Main ( string [] args ) { Console . WriteLine ( "Constructor trong C#" ); Console . WriteLine ( "---------------------" ); //tao doi tuong Line bang constructor Line line = new Line ( 10.0 ); Console . WriteLine ( "Chieu dai cua duong la: {0}" , line . getChieuDai ()); // thiet lap chieu dai cho duong line . setChieuDai ( 6.0 ); Console . WriteLine ( "Chieu dai cua duong la: {0}" , line . getChieuDai ()); Console . ReadKey (); } } } 

Compiling and running the above C # program will produce the following results:

Picture 4 of Class (Class) in C #

Destructor in C #

A destructor in C #, is a special member function of a class, executed whenever an object of that class exits the scope. A destructor has the same name as the class with a preceding (~) preceding and it can: not return a value or receive any parameters.

Destructor in C # can be very useful to free memory resources before exiting the program. Destruction cannot be inherited or overloaded.

The following example illustrates the concept of destructor in C #:

 using System ; namespace QTMCsharp { class Line { private double chieu_dai ; public Line () //constructor { Console . WriteLine ( "Doi tuong dang duoc tao." ); } ~ Line () // destructor { Console . WriteLine ( "Doi tuong dang bi xoa!!!" ); } public void setChieuDai ( double len ) { chieu_dai = len ; } public double getChieuDai () { return chieu_dai ; } static void Main ( string [] args ) { Console . WriteLine ( "Constructor trong C#" ); Console . WriteLine ( "---------------------" ); //tao doi tuong Line bang constructor Line line = new Line (); // thiet lap chieu dai cho duong line . setChieuDai ( 6.0 ); Console . WriteLine ( "Chieu dai cua duong la: {0}" , line . getChieuDai ()); Console . ReadKey (); } } } 

Compiling and running the above C # program will produce the following results:

Picture 5 of Class (Class) in C #

Static member of a Class in C #

We can define class members as static using the static keyword in C #. When we declare a class member as static , that is, no matter how many objects of the class are created, there is only one copy of the static member.

The static keyword implies that only one instance of a member exists for that class. Static variables are used to define constants because their values ​​can be obtained by calling that class without creating an instance of it. Static variables can be initialized outside the member function or class definition. You can also initialize static variables inside the class definition.

The following example illustrates the usage of static variables in C #: create two classes named ThanhVienStatic and TestCsharp respectively:

The ThanhVienStatic class: contains static members and methods

 using System ; namespace QTMCsharp { class ThanhVienStatic { public static int num ; //thanh vien static public void count () { num ++; } public int getNum () { return num ; } } } 

The TestCsharp class: contains the main () method to manipulate this ThanhVienStatic object

 dùng System; 
QTMCsharp namespace
{
public class TestCsharp
{
public static void Main (string [] args)
{
Console.WriteLine ("Static Member in C #");
Console.WriteLine ("------------------------ n");

// create the ThanhVienStatic doi tuong
ThanhVienStatic s1 = new ThanhVienStatic ();
ThanhVienStatic s2 = new ThanhVienStatic ();
// go to the office to count ()
s1.count ();
s1.count ();
s1.count ();
s2.count ();
s2.count ();
s2.count ();
Console.WriteLine ("Sign in for the number of numbers, s1 is: {0}", s1.getNum ());
Console.WriteLine ("Sign in for the number of numbers s2 is: {0}", s2.getNum ());

Console.ReadKey ();
}
}
}

Compiling and running the above C # program will produce the following results:

Picture 6 of Class (Class) in C #

You can also declare a static member function . These functions can only access static variables . Static functions can exist before objects are created. The following example illustrates the usage of static functions in C #: above you have created these two classes already, now just change the code of the two classes a bit like this:

The ThanhVienStatic class: contains static members and static methods

 using System ; namespace QTMCsharp { class ThanhVienStatic { public static int num ; // thanh vien static public void count () { num ++; } //phuong thuc static public static int getNum () { return num ; } } } 

The TestCsharp class: contains the main () method to manipulate this ThanhVienStatic object

 dùng System; 
QTMCsharp namespace
{
public class TestCsharp
{
public static void Main (string [] args)
{
Console.WriteLine ("The Static Method in C #");
Console.WriteLine ("------------------------ n");

// create the ThanhVienStatic doi tuong
ThanhVienStatic s = new ThanhVienStatic ();
// please call
s.count ();
s.count ();
s.count ();
Console.WriteLine ("Tri tri num: {0}", ThanhVienStatic.getNum ());

Console.ReadKey ();
}
}
}

Compiling and running the above C # program will produce the following results:

Picture 7 of Class (Class) in C #

Follow tutorialspoint

Previous article: Structure (Struct) in C #

Next lesson: Calculating inheritance in C #

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile