search
HomeBackend DevelopmentC#.Net TutorialC# Learning Diary 09---Data Type Struct Type

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  类型名
         {
                           访问修饰副  成员;
         }

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  
  
  
        }  
    }  
}

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;  
             
           }

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结构内的成员  
             
           }

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

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


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
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
Deploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideDeploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideApr 23, 2025 am 12:06 AM

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

Demystifying C# .NET: An Overview for BeginnersDemystifying C# .NET: An Overview for BeginnersApr 20, 2025 am 12:11 AM

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and the .NET Runtime: How They Work TogetherC# and the .NET Runtime: How They Work TogetherApr 19, 2025 am 12:04 AM

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.

C# .NET Development: A Beginner's Guide to Getting StartedC# .NET Development: A Beginner's Guide to Getting StartedApr 18, 2025 am 12:17 AM

To start C#.NET development, you need to: 1. Understand the basic knowledge of C# and the core concepts of the .NET framework; 2. Master the basic concepts of variables, data types, control structures, functions and classes; 3. Learn advanced features of C#, such as LINQ and asynchronous programming; 4. Be familiar with debugging techniques and performance optimization methods for common errors. With these steps, you can gradually penetrate the world of C#.NET and write efficient applications.

C# and .NET: Understanding the Relationship Between the TwoC# and .NET: Understanding the Relationship Between the TwoApr 17, 2025 am 12:07 AM

The relationship between C# and .NET is inseparable, but they are not the same thing. C# is a programming language, while .NET is a development platform. C# is used to write code, compile into .NET's intermediate language (IL), and executed by the .NET runtime (CLR).

The Continued Relevance of C# .NET: A Look at Current UsageThe Continued Relevance of C# .NET: A Look at Current UsageApr 16, 2025 am 12:07 AM

C#.NET is still important because it provides powerful tools and libraries that support multiple application development. 1) C# combines .NET framework to make development efficient and convenient. 2) C#'s type safety and garbage collection mechanism enhance its advantages. 3) .NET provides a cross-platform running environment and rich APIs, improving development flexibility.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.