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