C # struct

Source: Internet
Author: User

 

An alternative method for using classes is to use struct, which is a lightweight user-defined type. It is very similar to a class, but there are some differences:

 

Struct does not support inheritance or destructor;

 

Struct is a value type (class is a reference type );

 

Struct cannot declare the default constructor.

 

The struct is implicitly derived from the Object and is a value type (different from the class). This means that when an Object is created through the struct and assigned to another variable, this variable will contain a copy of the struct object.

 

The following is the definition of the Coordinate struct.

 

 

Public struct Coordinate

{

Public double latitude {get; set ;}

Public double longpolling {get; set ;}

}

You can also add a constructor to the struct as needed. The Code is as follows:

 

Public struct Coordinate

{

Public double latitude {get; set ;}

Public double longpolling {get; set ;}

Public Coordinate (double lat, double lng)

{

Latitude = lat;

Longpolling = lng;

}

}

Note that when you try to compile the application, the editor will send the following error

 

 

This restriction applies only to struct (the class does not have this problem). To solve this problem, you need to call the default constructor of the struct, as shown below:

 

 

Public struct Coordinate

{

Public double latitude {get; set ;}

Public double longpolling {get; set ;}

Public Coordinate (double lat, double lng): this ()

{

Latitude = lat;

Longpolling = lng;

}

}

 

From: column of Mars

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.