结构是一个值类型,是一个单位下不同数据类型的变量的集合。它几乎类似于一个类,因为它们都是用户定义的数据类型,并且都包含许多不同的数据类型。 C#提供了使用预定义数据类型的能力。但是,有时用户可能需要定义自己的数据类型,这也称为用户定义的数据类型。尽管它属于值类型,但用户可以根据要求对其进行修改,这就是为什么它也被称为用户定义的数据类型的原因。
定义结构:在C#中,结构是使用struct关键字定义的。使用struct关键字可以定义一个由不同数据类型组成的结构。结构还可以包含构造函数,常量,字段,方法,属性,索引器和事件等。
- 句法:
Access_Modifier struct structure_name { // Fields // Parameterized constructor // Constants // Properties // Indexers // Events // Methods etc. }
- 例子:
// C# program to illustrate the // Declaration of structure using System; namespace ConsoleApplication { // Defining structure public struct Person { // Declaring different data types public string Name; public int Age; public int Weight; } class Geeks { // Main Method static void Main(string[] args) { // Declare P1 of type Person Person P1; // P1's data P1.Name = "Keshav Gupta"; P1.Age = 21; P1.Weight = 80; // Displaying the values Console.WriteLine("Data Stored in P1 is " + P1.Name + ", age is " + P1.Age + " and weight is " + P1.Weight); } } }
输出:Data Stored in P1 is Keshav Gupta, age is 21 and weight is 80
说明:在上面的代码中,使用数据成员Name , Age和Weight创建了一个名为“ Person”的结构。在主方法中,创建了结构类型为Person的P1 。现在,P1可以在。(点)运算符的帮助下访问其数据成员。
复制结构:在C#中,用户可以使用’=’(赋值)运算符将一个结构对象复制到另一个结构对象中。
- 句法:
Structure_object_destination = structure_object_source;
- 例子:
// C# program to illustrate copy the structure using System; namespace ConsoleApplication { // Defining structure public struct Person { // Declaring different data types public string Name; public int Age; public int Weight; } class Geeks { // Main Method static void Main(string[] args) { // Declare P1 of type Person Person P1; // P1's data P1.Name = "Keshav Gupta"; P1.Age = 21; P1.Weight = 80; // Declare P2 of type Person Person P2; // Copying the values of P1 into P2 P2 = P1; // Displaying the values of P1 Console.WriteLine("Values Stored in P1"); Console.WriteLine("Name: " +P1.Name); Console.WriteLine("Age: " +P1.Age); Console.WriteLine("Weight: " +P1.Weight); Console.WriteLine(""); // Displaying the values of P2 Console.WriteLine("Values Stored in P2"); Console.WriteLine("Name: " +P2.Name); Console.WriteLine("Age: " +P2.Age); Console.WriteLine("Weight: " +P2.Weight); } } }
输出:Values Stored in P1 Name: Keshav Gupta Age: 21 Weight: 80 Values Stored in P2 Name: Keshav Gupta Age: 21 Weight: 80
说明:借助P1初始化struct Person的数据成员,并且可以使用’=’(赋值运算符)将数据成员的值由P1复制到P2。
结构嵌套: C#允许将一个结构声明为另一种结构,此概念称为该结构的嵌套。
- 例子:
// C# program to illustrate Nesting of structures using System; namespace ConsoleApplication { // first structure defined // with public modifier public struct Address { // data member of Address structure public string City; public string State; } // Another structure struct Person { // data member of Person structure public string Name; public int Age; // Nesting of Address structure // by creating A1 of type Address public Address A1; } class Geeks { // Main method static void Main(string[] args) { // Declare p1 of type Person Person p1; // Assigning values to the variables p1.Name = "Raman"; p1.Age = 12; // Assigning values to the nested // structure data members p1.A1.City = "ABC_City"; p1.A1.State = "XYZ_State"; Console.WriteLine("Values Stored in p1"); Console.WriteLine("Name: " +p1.Name); Console.WriteLine("Age: " +p1.Age); Console.WriteLine("City: " +p1.A1.City); Console.WriteLine("State: " +p1.A1.State); } } }
输出:Values Stored in p1 Name: Raman Age: 12 City: ABC_City State: XYZ_State
有关结构的要点:
- 一旦结构超出范围,它将自动释放。
- 比堆类型更容易,更快速地创建。
- 使用结构,将变量的值复制到堆栈变得很容易。
- struct是值类型,而class是引用类型。
结构和类别之间的差异:
Category | Structure | Class |
---|---|---|
Data Type | Value Type | Reference type |
Assignment Operation | Copies the value | Copies the reference |
Parameterless Constructors | Not Allowed | Allowed |
Inheritance | Not supported | Always supported |