Structure (Struct) in C #

In C #, a structure is a data type. It helps you create a single variable that keeps relevant data of diverse data types. The keyword struct in C # is used to create a structure.

In C #, a structure is a data type. It helps you create a single variable that keeps relevant data of diverse data types. The keyword struct in C # is used to create a structure .

Structures are used to represent a record. Suppose you want to keep track of books in a library. You may want to track the following attributes of each book:

  1. Book title
  2. Author
  3. Kind
  4. ID (book code)

Defining structure in C #

To define the structure, you must use the struct command. The struct statement defines a new data type, with more than one member in your program.

The following example is how you declare the Book structure:

 struct Books { public string ten_sach ; public string tac_gia ; public string the_loai ; public int ma_sach ; }; 

The following program illustrates how to use the above structure in C #:

 using System ; //cau truc book struct Book { public string ten_sach ; public string tac_gia ; public string the_loai ; public int ma_sach ; }; namespace QTMCsharp { class TestCsharp { static void Main ( string [] args ) { Console . WriteLine ( "Struct trong C#" ); Console . WriteLine ( "----------------------------n" ); Book Book1 ; /* khai bao Book1 thuoc kieu cau truc Book */ Book Book2 ; /* khai bao Book2 thuoc kieu cau truc Book */ /* thong tin chi tiet ve Book1 */ Book1 . ten_sach = "English Grammar in Use" ; Book1 . tac_gia = "Raymond Murphy" ; Book1 . the_loai = "Tieng Anh" ; Book1 . ma_sach = 6495407 ; /* thong tin chi tiet ve Book2 */ Book2 . ten_sach = "Toan hoc cao cap" ; Book2 . tac_gia = "Tran Van A" ; Book2 . the_loai = "Toan hoc" ; Book2 . ma_sach = 6495700 ; /* in cac thong tin cua Book1*/ Console . WriteLine ( "In thong tin cua cuon sach 1:" ); Console . WriteLine ( "Ten sach: {0}" , Book1 . ten_sach ); Console . WriteLine ( "Tac gia: {0}" , Book1 . tac_gia ); Console . WriteLine ( "The loai: {0}" , Book1 . the_loai ); Console . WriteLine ( "Ma sach: {0}" , Book1 . ma_sach ); /* in cac thong tin cua Book2 */ Console . WriteLine ( "nIn thong tin cua cuon sach 2:" ); Console . WriteLine ( "Ten sach: {0}" , Book2 . ten_sach ); Console . WriteLine ( "Tac gia: {0}" , Book2 . tac_gia ); Console . WriteLine ( "The loai: {0}" , Book2 . the_loai ); Console . WriteLine ( "Ma sach: {0}" , Book2 . ma_sach ); 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.

Characteristics of structure in C #

Above, you used a simple Books structure. Structures in C # are quite different from the traditional structure in C or C ++. The structure in C # has the following characteristics:

The structure can have methods, fields, indexers, properties, operator methods, and events.

The structure may have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.

Unlike Classes, the structure cannot inherit from another structure or class.

The structure cannot be used as a basis for structures or other classes.

A structure can deploy one or more interfaces.

Structure members cannot be defined as abstract, virtual, or protected.

When you create a Struct object by using the new operator, it takes the created object and the appropriate constructor is called. Unlike the class, the structure can be initialized without using the new operator.

If the new operator is not used, then the fields are not assigned and the object cannot be used until all fields are initialized.

Differentiate Class and Structure in C #

Classes and Structures in C # have some basic differences:

Classes are reference types, and structures are value types.

The structure does not support inheritance.

The structure has no default constructor.

From the above points, we rewrite the example above:

 using System ; struct Book { private string ten_sach ; private string tac_gia ; private string the_loai ; private int ma_sach ; public void nhapGiaTri ( string t , string a , string s , int id ) { ten_sach = t ; tac_gia = a ; the_loai = s ; ma_sach = id ; } public void display () { Console . WriteLine ( "Tieu de: {0}" , ten_sach ); Console . WriteLine ( "Tac gia: {0}" , tac_gia ); Console . WriteLine ( "The loai: {0}" , the_loai ); Console . WriteLine ( "Ma sach: {0}" , ma_sach ); } }; public class TestCsharp { public static void Main ( string [] args ) { Console . WriteLine ( "Struct trong C#" ); Console . WriteLine ( "------------------------n" ); Book Book1 = new Book (); /* Khai bao Book1 thuoc kieu cau truc Book */ Book Book2 = new Book (); /* Khai bao Book2 thuoc kieu cau truc Book */ /* thong tin Book1 */ Book1 . nhapGiaTri ( "English Grammer in Use" , "Raymond Murphy" , "Tieng Anh" , 6495407 ); /* thong tin book2 */ Book2 . nhapGiaTri ( "Toan hoc cao cap" , "Tran Van A" , "Toan hoc" , 6495700 ); /* In thong tin Book1 */ Console . WriteLine ( "In thong tin cua cuon sach 1:" ); Book1 . display (); /* In thong tin Book2 */ Console . WriteLine ( "nIn thong tin cua cuon sach 2:" ); Book2 . display (); 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.

Follow tutorialspoint

Previous lesson: String (String) in C #

Next lesson: Class (Class) in C #

5 ★ | 1 Vote