C # structure struct summary,

Source: Internet
Author: User

C # structure struct summary,

  • The struct type is a value type, which is usually used to encapsulate small correlation variable groups.
  • Struct can contain members: constructors, constants, fields, methods, attributes, cited objects, events, and nested types. If the preceding types are required at the same time, you should consider using classes instead of struct.
  • The structure can inherit from classes (single inheritance, different from C ++) and implementation interfaces. A single structure cannot inherit from a single structure.
  • The structure member cannot be declared as protected (because the structure cannot be inherited)
  • Struct allocates memory space on the stack
  • Struct declarative position: it is in the same level as the class or inside the class, but cannot be in the method body.
  • You cannot declare a new constructor without parameters.
  • Struct can declare a static constructor, but this function cannot have an access modifier and this function has no parameters.
  • The static methods of non-constructor in the structure must be public in addition to the access modifier. The other methods are the same as those declared in the class.
  • The struct instance statement does not need new, which is different from the reference types such as class.

Example:

Struct S {private int d = 0; public int D {get {return d;} set {d = value ;}} public S (int p) {D = p ;} public static int Sum (int a, int B) {return a + B ;}}

 

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.