C # struct struct summary

Source: Internet
Author: User

C # struct struct summary

  1. Structs are value types and are sealed and cannot be inherited and derived.

  2. Structure statement:

struct Structname

{

Memberdeclaration

}

  

struct POINT

{

public int x; Field initialization is not allowed in the structure

Pub int y;

}

  3. The structure has the following characteristics

(1) structs are very similar to classes, but structs are value types and classes are reference types.

(2) struct instantiation may not be applicable to the new operator. However, after the settings data members are displayed, their values can be called.

(3) The system has provided an implicit constructor (no parameters) for the structure, so the struct declaration constructor must be a parameter.

(4) Structs do not support inheritance, and structures cannot derive from other structures.

(5) When declaring a struct, it is not allowed to assign a value at the time of initialization of the field.

  4. Programming examples of structures  

struct information
{
private string colour;
public string colour
{
get {return colour;}
set {colour = value;}
}

Private double hight;
Public double Hight
{
set {hight = value;}
get {return hight;}
}

private string gender;
public string Gender
{
set {gender = value;}
get {return gender;}
}

Public information (string colour, double hight, string gender)
{
Constructors with parameters, you must assign values to all variables!!
This.colour = colour;
This.gender = gender;
This.hight = hight;
}
}
Class Program
{
static void Main (string[] args)
{
Information info = new information ("Red", 180.1, "man");
Information info1 = info;
Console.WriteLine ("Colour: {0}\nhight: {1}\ngender: {2}", Info1. Colour, info1. Hight, Info1. Gender);
}
}

  

C # struct struct summary

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.