C # struct BODY

Source: Internet
Author: User

Defining structures

In order to define a structure, you must use a struct statement. A struct statement defines a new data type with multiple members for a program.

In C #, structs are value type data structures. It enables a single variable to store relevant data for various data types. The struct keyword is used to create structures.

struct student{   publicstring  name;     Public int Xuehao;     Public Double Fenshu;   };  
Features of C # architecture
  • Structs can have methods, fields, indexes, properties, operator methods, and events.
  • A struct can define a constructor, but it cannot define a destructor. However, you cannot define a default constructor for the struct. The default constructor is automatically defined and cannot be changed.
  • Unlike classes, structs cannot inherit from other structures or classes.
  • Structs cannot be the infrastructure of other structures or classes.
  • A struct can implement one or more interfaces.
  • Struct members cannot be specified as abstract, virtual, or protected.
  • When you create a struct object using the New operator, the appropriate constructor is called to create the structure. Unlike classes, structs can be instantiated without the use of the NEW operator.
  • If you do not use the New operator, the field is assigned only after all the fields have been initialized and the object is used.
  • Class VS Structure

    Classes and structs have the following basic differences:

    • A class is a reference type, and a struct is a value type.
    • The structure does not support inheritance.
    • Structs cannot declare a default constructor.
    • usingSystem; structstudent{Private stringname; Private intXuehao; Private DoubleChengji; }    Public voidGetValues (stringNintXDoublec) {name=T; Xuehao=x; Chengji=C; }    Public voiddisplay () {Console.WriteLine ("name: {0}", name); Console.WriteLine ("Xuehao: {0}", Xuehao); Console.WriteLine ("Chengji: {0}", Chengji);  }};  Public classstudent{ Public Static voidMain (string[] args) {Student S=NewStudent ();/*declaration s, type student*/Student S2=NewStudent ();/*declaration s2, type student*/      /*S1 Details*/S1.getvalues ("Xuehao",      "name","Chengji",+Xuehao,name,chengji); /*s 2 details*/S2.getvalues ("Xuehao",      "Chengji","name", +Xuehao,name,chengji); /*Print S1 information*/S1.display (); /*Print s2 Information*/S2.display ();   Console.readkey (); }}

C # struct BODY

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.