C # Learning Journal---structure (struct) type of data type

Source: Internet
Author: User
struct type (struct type) of numeric type:

After the simple type of learning, we are doing some of the commonly used data operations, word processing, seems to be enough, but when we encounter some complex data types, for example, the class management system to enter each student's name, age, telephone number, address. If you follow the simple data type we learned earlier, each input to a student's information will be stored with 4 different variables, so the workload is too large, but also not intuitive, it is easy to confuse.

Defining a struct structure in C + + is used to put together a set of related information and to organize related variables into a single entity. Take the example above to make an analogy, when I enter a student information can apply for a "box" (structure type), we in the "box" to put his name, age, telephone number, address (structure of members), and then give the box a name, such as the name "Zhang San Box", When you want to view or change the Zhang San information, just find the box to call in the members.

C # Fully references the struct types in C/s + +, so his usage and functionality are the same.

C # defines the struct type format:    access modifier struct  type         name                           {Access decorated Vice  member;         }

Access modifiers I'll explore it in detail, briefly saying his nature, there are five types of access modifiers in C #: Public, private, protected, internal, protected internal. There are no restrictions on public access, and we are now using public.

Assign a value to a member in a struct type, and the query method is the same as a C + +, a struct variable name. member name = value, such as in the example above Zhang San can be the case of assigning Zhang san. Age = 18;

I still use the above example to write a code, input 2 students information, and lose:

Using System;  Using System.Collections.Generic;  Using System.Linq;    Using System.Text; Namespace Example {class Program {public struct Student {public string n               Ame               public uint Age;               public ULONG Tel;                            public string address;  } static void Main (string[] args) {Student Firstperson, Secperson;               Define two variables of the student type, which is to apply for 2 ' chests ' and take the name #region Enter the first Student information Console.WriteLine ("\ t (first Student information)");              Console.WriteLine ("Enter Name:");              Firstperson.name = Console.ReadLine ();              Console.WriteLine ("Input Age:"); Firstperson.age = uint. Parse (Console.ReadLine ());              Force type conversion Converts a string type to uint Console.WriteLine ("Enter phone number:"); Firstperson.tel = ulong.              Parse (Console.ReadLine ());              Console.WriteLine ("Please enter Address:"); Firstperson.address = Console.readLine ();              #endregion #region Enter the second student information Console.WriteLine ("\ t (second student information)");              Console.WriteLine ("Enter Name:");              Secperson.name = Console.ReadLine ();              Console.WriteLine ("Input Age:"); Secperson.age = uint.               Parse (Console.ReadLine ());              Console.WriteLine ("Enter phone number:"); Secperson.tel = ulong.  Parse (Console.ReadLine ());              Coercion type conversion converts a string type to ULONG type Console.WriteLine ("Please enter Address:");              Secperson.address = Console.ReadLine (); #endregion #region Output The information for these two students Console.WriteLine ("1. Name: {0}\t Age: {1}\t Tel: {2}\t address: {3}", Firstperson              . name,firstperson.age,firstperson.tel,firstperson.address); Console.WriteLine ("2. Name: {0}\t Age: {1}\t Tel: {2}\t address: {3}", Secperson.name, Secperson.age, Secperson.tel,               secperson.address); #endregion}}}

Output Result:

Clearer code structure when editing code I joined the #region and #enregion function is to shrink the written code, that is:

The struct type is unlimited for his member type, the type can be the same or different, there is no limit to the number of members inside, such as we can also add a gender member char sex;

public struct Student             {public              string name;              public uint Age;              public ULONG Tel;              public string address;              public char sex;                          }

Even the struct type can be used as a member of another struct type, and there is no problem. Or the Student information example above, we change the address of the member to a structure type, where the address structure contains nationality, province, city, Street.

Class program     {public        struct Student             {public              string name;              public uint Age;              public ULONG Tel;           Public struct address              {public              string nationality;              public string province;   Variable names can be used in the public string city of Chinese characters              ;              public string Street;              public char sex;                public address Ad;  Declaring an address type variable an ad external function accesses an ad to access a member within the address structure                          }

Similarly, if you want to assign values to members within the address structure, the same method

  Firstperson.ad. City = Console.ReadLine ();  firstperson.ad city = "Chengdu"; it's all OK.


The above is the C # Learning Diary---data type structure (struct) type content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.