Share via


Using Structs (C# Programming Guide) 

The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color. Although it is possible to represent a point as a class, a struct is more efficient in some scenarios. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object; in this case, a struct would be less expensive. Since the .NET Framework contains an object called Point, we'll call our struct "CoOrds" instead.

public struct CoOrds
{
    public int x, y;

    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}

It is an error to declare a default (parameterless) constructor for a struct. A default constructor is always provided to initialize the struct members to their default values. It is also an error to initialize an instance field in a struct.

When you create a struct object using the new operator, it gets created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the fields will remain unassigned and the object cannot be used until all of the fields are initialized.

There is no inheritance for structs as there is for classes. A struct cannot inherit from another struct or class, and it cannot be the base of a class. Structs, however, inherit from the base class Object . A struct can implement interfaces, and it does that exactly as classes do.

Unlike C++, you cannot declare a class using the keyword struct. In C#, classes and structs are semantically different. A struct is a value type, while a class is a reference type. For more information, see Value Types.

Unless you need reference-type semantics, small classes may be more efficiently handled by the system as a struct.

Example 1

This example demonstrates struct initialization using both default and parameterized constructors.

public struct CoOrds
{
    public int x, y;

    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}
// Declare and initialize struct objects.
class TestCoOrds
{
    static void Main()
    {
        // Initialize:   
        CoOrds coords1 = new CoOrds();
        CoOrds coords2 = new CoOrds(10, 10);

        // Display results:
        System.Console.Write("CoOrds 1: ");
        System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);

        System.Console.Write("CoOrds 2: ");
        System.Console.WriteLine("x = {0}, y = {1}", coords2.x, coords2.y);
    }
}

Output

CoOrds 1: x = 0, y = 0

CoOrds 2: x = 10, y = 10

Example 2

This example demonstrates a feature that is unique to structs. It creates a CoOrds object without using the new operator. If you replace the word struct with the word class, the program will not compile.

public struct CoOrds
{
    public int x, y;

    public CoOrds(int p1, int p2)
    {
        x = p1;
        y = p2;
    }
}
// Declare a struct object without "new."
class TestCoOrdsNoNew
{
    static void Main()
    {
        // Declare an object:
        CoOrds coords1;

        // Initialize:
        coords1.x = 10;
        coords1.y = 20;

        // Display results:
        System.Console.Write("CoOrds 1: ");
        System.Console.WriteLine("x = {0}, y = {1}", coords1.x, coords1.y);
    }
}

Output

CoOrds 1: x = 10, y = 20

See Also

Reference

Objects, Classes and Structs (C# Programming Guide)

Concepts

C# Programming Guide
Structs (C# Programming Guide)