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

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

Jan 20, 2017 pm 01:37 PM

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!


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Active Directory with C# Active Directory with C# Sep 03, 2024 pm 03:33 PM

Guide to Active Directory with C#. Here we discuss the introduction and how Active Directory works in C# along with the syntax and example.

Access Modifiers in C# Access Modifiers in C# Sep 03, 2024 pm 03:24 PM

Guide to the Access Modifiers in C#. We have discussed the Introduction Types of Access Modifiers in C# along with examples and outputs.

Random Number Generator in C# Random Number Generator in C# Sep 03, 2024 pm 03:34 PM

Guide to Random Number Generator in C#. Here we discuss how Random Number Generator work, concept of pseudo-random and secure numbers.

C# Data Grid View C# Data Grid View Sep 03, 2024 pm 03:32 PM

Guide to C# Data Grid View. Here we discuss the examples of how a data grid view can be loaded and exported from the SQL database or an excel file.

C# StringReader C# StringReader Sep 03, 2024 pm 03:23 PM

Guide to C# StringReader. Here we discuss a brief overview on C# StringReader and its working along with different Examples and Code.

Patterns in C# Patterns in C# Sep 03, 2024 pm 03:33 PM

Guide to Patterns in C#. Here we discuss the introduction and top 3 types of Patterns in C# along with its examples and code implementation.

C# Serialization C# Serialization Sep 03, 2024 pm 03:30 PM

Guide to C# Serialization. Here we discuss the introduction, steps of C# serialization object, working, and example respectively.

C# StringWriter C# StringWriter Sep 03, 2024 pm 03:23 PM

Guide to C# StringWriter. Here we discuss a brief overview on C# StringWriter Class and its working along with different Examples and Codes.

See all articles