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

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату