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:
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:
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:
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:
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:
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:
Follow tutorialspoint
Previous article: Structure (Struct) in C #
Next lesson: Calculating inheritance in C #