Home > Backend Development > C#.Net Tutorial > C# Learning Diary 09---Data Type Struct Type

C# Learning Diary 09---Data Type Struct Type

黄舟
Release: 2017-01-20 13:37:01
Original
1726 people have browsed it

Structural type of numerical type (struct type):

After studying the previous simple types, we are doing some common data operations and word processing, which seems to be enough, but when we encounter some Complex data types, for example, each student's name, age, phone number, and address need to be entered in the class management system. If we handle it according to the simple data types we learned earlier, each time a student's information is entered, it will be stored in 4 different variables. This will cause too much work, is not intuitive, and is easy to confuse.

The struct structure is defined in C/C++ to put a set of related information together and organize related variables into a single entity. Take the above example as an analogy. When I enter a student's information, I can apply for a "box" (structure type). We put his name, age, phone number, and address in the "box" ( structure), and then give the box a name, such as "Zhang San's box". When you want to view or change Zhang San's information, you only need to find this box and call out the members inside.

C# completely references the struct type in C/C++, so its usage and functions are the same.

C#定义Struct类型格式:
    访问修饰符 struct  类型名
         {
                           访问修饰副  成员;
         }
Copy after login

I will explore the access modifiers in detail later and briefly talk about their properties. C# has five access modifiers: public, private, protected, internal, and protected internal. Among them, public is not subject to any restrictions and has shared access. At present, we all use public.

The member assignment and query methods in the structure type are the same as C/C++, structure variable name. member name = value, such as the above In Zhang San's example, the value can be assigned like this Zhang San's box. Age = 18;

I still use the above example to write a code, enter the information of 2 students, and output:

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace Example  
{   
    class Program  
    {  
       public struct Student  
            {  
             public string name;  
             public uint age;  
             public ulong tel;  
             public string address;  
              
            }  
         
        static void Main(string[] args)  
        {  
            Student Firstperson, Secperson;  //定义两个Student 类型的变量也就是申请2个‘箱子’并取了名字  
            #region        录入第一个学生信息  
  
            Console.WriteLine("\t(第一个学生信息)");  
            Console.WriteLine("输入姓名:");  
            Firstperson.name = Console.ReadLine();  
            Console.WriteLine("输入年龄:");  
            Firstperson.age = uint.Parse(Console.ReadLine()); //强制类型转换  将String 类型转换为uint   
            Console.WriteLine("输入电话号码:");  
            Firstperson.tel = ulong.Parse(Console.ReadLine());  
            Console.WriteLine("请输入地址:");  
            Firstperson.address = Console.ReadLine();  
            #endregion  
 
            #region 录入第二个学生信息  
            Console.WriteLine("\t(第二个学生信息)");  
            Console.WriteLine("输入姓名:");  
            Secperson.name = Console.ReadLine();  
            Console.WriteLine("输入年龄:");  
            Secperson.age = uint.Parse(Console.ReadLine());   
            Console.WriteLine("输入电话号码:");  
            Secperson.tel = ulong.Parse(Console.ReadLine());  //强制类型转换  将String 类型转换为ulong类型   
            Console.WriteLine("请输入地址:");  
            Secperson.address = Console.ReadLine();  
            #endregion  
 
            #region  输出这两个学生的信息  
            Console.WriteLine("1.姓名:{0}\t年龄:{1}\t电话:{2}\t地址:{3}",Firstperson.name,Firstperson.age,Firstperson.tel,Firstperson.address);  
            Console.WriteLine("2.姓名:{0}\t年龄:{1}\t电话:{2}\t地址:{3}", Secperson.name, Secperson.age, Secperson.tel, Secperson.address);  
 
            #endregion  
  
  
        }  
    }  
}
Copy after login

Output result:

C# Learning Diary 09---Data Type Struct Type

When editing the code, in order to make the code structure clearer, I added #region and #enregion. The function is to shrink the written code. That's it:

C# Learning Diary 09---Data Type Struct Type

The structure type has no restrictions on its member types. The types inside can be the same or different, and there are no restrictions on the number of members inside, such as We can also add the gender member char sex;

public struct Student  
           {  
            public string name;  
            public uint age;  
            public ulong tel;  
            public string address;  
            public char sex;  
             
           }
Copy after login

We can even use the structure type as a member of another structure type, which is no problem. . Still using the student information example above, we change the member address into a structure type, where the address structure contains nationality, province, city, and street.

class Program  
   {  
      public struct Student  
           {  
            public string name;  
            public uint age;  
            public ulong tel;  
         public struct address  
            {  
            public string nationality;  
            public string 省份;   // 变量名可以用汉字  
            public string 市;  
            public string street;  
              
             }  
            public char sex;  
  
            public address Ad;  //声明 一个 address类型变量Ad 外部函数通过访问Ad才能访问 address结构内的成员  
             
           }
Copy after login

Similarly, if you want to assign values ​​to members in the address structure, use the same method

  Firstperson.Ad.市 = Console.ReadLine();  //  Firstperson.Ad.市 = "成都";都是可以的。
Copy after login


The above is the structure of C# learning diary 09---data type ( Struct) type content, please pay attention to the PHP Chinese website (www.php.cn) for more related content!


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template