C#_ basic Structure struct (eight)

Source: Internet
Author: User

In general, the use of structs is not recommended.

The structure can contain many different types of fields, methods

Define a structure:

1   Public struct Person2         {3              Public string_name;//The naming convention field is underlined before4             Public   int_age;5             Public   Char_gender;6             PublicQqstate _state;//Enum Type7}

struct declaration: Calling

1  // declares a struct variable, essentially the same as an enumeration, where int is a variable, 2 Person              Zhangsan; 3              " Zhangsan " ; 4               - ; 5              Zhangsan._state = qqstate.busy; 6              ' male ';

The structure can also have methods:

1   Public struct Person2         {3              Public string_name;//The naming convention field is underlined before4             Public   int_age;5             Public   Char_gender;6             PublicQqstate _state;//Enum Type7              Public voidRun ()8            {9Console.WriteLine ("Run");Ten}

Call:

Zhangsan. Run ();

Structural Features:

Structs and classes are different and cannot be new objects. Declaring objects directly can

Structure is a value type, so the object is only in the stack and does not open space in the heap

struct does not have a default constructor.

Structure Construction Method: You must assign values to all the fields in the structure to

1  /// <summary>2         ///case with no fields3         /// </summary>4          Public structStudent5         {6              PublicStudent ()7             {8 9             }Ten              Public voidStudy () One             { A  -             } -}

Structure New object:

Student s = new Student ();

1  /// <summary>2         ///structure with Fields3         /// </summary>4          Public struct Person5         {6              Public string_name;//The naming convention field is underlined before7              Public int_age;8              Public Char_gender;9              PublicQqstate _state;//Enum TypeTen            One              PublicPerson (stringname) A             { -                  This. _name =name; -                  This. _age = A; the                  This. _gender ='male'; -                  This. _state =qqstate.busy; -  -             } +  -              +}

C#_ basic Structure struct (eight)

Related Article

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.