Monday, November 22, 2010

CTS Structure Types

The concept of a structure is also formalized under the CTS. If you have a C background, you should be
pleased to know that these user-defined types (UDTs) have survived in the world of .NET (although they
behave a bit differently under the hood). Simply put, a structure can be thought of as a lightweight class
type having value-based semantics. For more details on the subtleties of structures, see Chapter 4.
Typically, structures are best suited for modeling geometric and mathematical data and are created in
C# using the struct keyword.// A C# structure type.
struct Point
{
// Structures can contain fields.
public int xPos, yPos;
// Structures can contain parameterized constructors.
public Point(int x, int y)
{ xPos = x; yPos = y;}
// Structures may define methods.
public void PrintPosition()
{
Console.WriteLine("({0}, {1})", xPos, yPos);
}
}

No comments:

Post a Comment