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 .
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:
- Book title
- Author
- Kind
- 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 #
You should read it
- What is the best URL structure for SEO?
- What is Data Structure?
- What is Silo Structure? Advantages and disadvantages of silos and effective alternatives
- Queue data structure (Queue)
- Stack data structure (Stack)
- New achievement: TSP chip structure can run 1 million billion operations per second
- Learn about URLs
- Add Structure to Diagram in Visio 2010 using List and Container
May be interested
- Learn about URLsyou must have heard of the url but don't know exactly what it is, how the structure is and what the composition is. this article will therefore give you an overview of the url and its structure.
- C # program structurebefore learning how to build c # programming language blocks, we should explore the basic c # program structure.
- Heap data structuredata structure the heap is a special case of a balanced binary tree data structure, where the root node's key is compared to its children and arranged accordingly.
- Nested Structure in Golanggo language allows nested structures. a structure that is a field of another structure is called nested structure.
- How to fix 'Critical Structure Corruption' blue screen error on Windows 10critical structure corruption is a blue screen of death (bsod) error that can be encountered at any time while you are using a windows computer.
- Add Structure to Diagram in Visio 2010 using List and Containerin the microsoft visio 2010 model design application, one of the most used features is to assign a structure - structure to the chart - diagrams using containers, lists and callouts. in the following article, we will go deeper and learn more about the above concepts and how to use them accordingly ...
- Structure & Principle of operation of air conditioner, air conditionerjoin tipsmake.com to learn about the structure and operation principles of air conditioning and air conditioning - familiar devices that help us overcome the hot summer days.
- What is a microwave? Structure and utility of a microwave in lifea microwave or microwave is a popular kitchen tool that makes cooking food faster and more convenient. so what is a microwave? structure and use like?
- Your bone structure also affects appetitea new study shows that the human skeleton is not only a muscle support structure, but also other tissues that affect appetite.
- Graph data structure (Graph)a graph (graph) is a form of visual representation of a set of objects in which pairs of objects are connected by links. interconnected objects are represented by points called vertices, and links that connect vertices are called edges.