: C# 2008 Programmer

Structures

Structures

An alternative to using classes is to use a struct (for structure). A struct is a lightweight user-defined type that is very similar to a class, but with some exceptions:

?Structs do not support inheritance or destructors.

?A struct is a value type (class is a reference type).

?A struct cannot declare a default constructor.

Structs implicitly derive from object and unlike classes, a struct is a value type. This means that when an object is created from a struct and assigned to another variable, the variable will contain a copy of the struct object.

Like classes, structs support constructor, properties, and methods. The following code shows the definition for the Coordinate struct:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
}
}
public struct Coordinate {
public double latitude { get; set; }
public double longitude { get; set; }
}

The Coordinate struct contains two properties (defined using the automatic properties feature). You can add a constructor to the struct if you want:

public struct Coordinate {
public double latitude { get; set; }
public double longitude { get; set; }
public Coordinate(double lat, double lng) {
latitude = lat;
longitude = lng;
}
}

Remember, a struct cannot have a default constructor.

Note that the compiler will complain with the message "Backing field for automatically implemented property 'Coordinate.latitude' must be fully assigned before control is returned to the caller" when you try to compile this application. This restriction applies only to structs (classes won't have this problem). To resolve this, you need to call the default constructor of the struct, like this:

public struct Coordinate {
public double latitude { get; set; }
public double longitude { get; set; }
public Coordinate(double lat, double lng) :
this() {
latitude = lat;
longitude = lng;
}
}

You can also add methods to a struct. The following shows the ToString() method defined in the Coordinate struct:

public struct Coordinate {
public double latitude { get; set; }
public double longitude { get; set; }
public Coordinate(double lat, double lng) :
this() {
latitude = lat;
longitude = lng;
}
public override string ToString() {
return latitude + "," + longitude;
}
}

To use the Coordinate struct, create a new instance using the new keyword and then initialize its individual properties:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) {
Coordinate pt1 = new Coordinate();
pt1.latitude = 1.33463167;
pt1.longitude = 103.74697;
}
}

Or you can use the object initializer feature:

private void Form1_Load(object sender, EventArgs e) {
//...
Coordinate pt2 = new Coordinate() {
latitude = 1.33463167,
longitude = 103.74697
};
}

Because structs are value types, assigning one struct to another makes a copy of its value, as the following code sample shows:

private void Form1_Load(object sender, EventArgs e) {
//...
Coordinate pt2 = new Coordinate() {
latitude = 1.33463167,
longitude = 103.74697
};
Coordinate pt3;
pt3 = pt2;
Console.WriteLine("After assigning pt2 to pt3");
Console.WriteLine("pt2: {0}", pt2.ToString());
Console.WriteLine("pt3: {0}", pt3.ToString());
pt3.latitude = 1.45631234;
pt3.longitude = 101.32355;
Console.WriteLine("After changing pt3");
Console.WriteLine("pt2: {0}", pt2.ToString());
Console.WriteLine("pt3: {0}", pt3.ToString());
}

Here's the program's output:

After assigning pt2 to pt3
pt2: 1.33463167,103.74697
pt3: 1.33463167,103.74697
After changing pt3
pt2: 1.33463167,103.74697
pt3: 1.45631234,101.32355

Notice that after changing the properties of pt3, the latitude and longitude properties of pt2 and pt3 are different.

Memory Allocation

When you use the new keyword to create an instance of a class, the object will be allocated on the heap. When using structs, the struct object is created on the stack instead. Because of this, using structs yields better performance gains. Also, when passing a struct to a method, note that it is passed by value instead of passed by reference.

In general, use classes when dealing with large collections of data. When you have smaller sets of data to deal with, using structs is more efficient.


: 0.498. /Cache: 2 / 0