Книга: C# 2008 Programmer

Generic Structs

Generic Structs

Generics can also be applied to structs. For example, suppose that you have a Coordinate struct defined as follows:

public struct Coordinate {
 public int x, y, z;
}

The coordinates for the Coordinate struct takes in int values. You can use generics on the Coordinate struct, like this:

public struct Coordinate<T> {
 public T x, y, z;
}

To use int values for the Coordinate struct, you can do so via the following statements:

Coordinate<int> pt1;
pt1.x = 5;
pt1.y = 6;
pt1.z = 7;

To use float values for the Coordinate struct, utilize the following statements:

Coordinate<float> pt2;
pt2.x = 2.0F;
pt2.y = 6.3F;
pt2.z = 2.9F;

Оглавление книги


Генерация: 1.130. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз