Export (0) Print
Expand All
124 out of 156 rated this helpful - Rate this topic

struct

A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types. The declaration of a struct takes the following form:

[attributes] [modifiers] struct identifier [:interfaces] body [;]

where:

attributes (Optional)
Additional declarative information. For more information on attributes and attribute classes, see 17. Attributes.
modifiers (Optional)
The allowed modifiers are new and the four access modifiers.
identifier
The struct name.
interfaces (Optional)
A list that contains the interfaces implemented by the struct, all separated by commas.
body
The struct body that contains member declarations.

Remarks

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, the struct is less expensive.

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 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 on the features of value types, see Value Types.

Unless you need reference type semantics, a class that is smaller than 16 bytes may be more efficiently handled by the system as a struct.

For more information on structs, see 11. Structs and the Structs Tutorial.

In Managed Extensions for C++, the equivalents to a C# class and a C# struct are as follows:

C# Managed Extensions for C++ For more information
class __gc struct
or
__gc class
__gc keyword
struct __value struct
or
__value class
__value keyword

Example 1

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

// keyword_struct.cs
// struct declaration and initialization
using System;
public struct Point 
{
   public int x, y;

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

class MainClass 
{
   public static void Main()  
   {
      // Initialize:   
      Point myPoint = new Point();
      Point yourPoint = new Point(10,10);

      // Display results:
      Console.Write("My Point:   ");
      Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);
      Console.Write("Your Point: ");
      Console.WriteLine("x = {0}, y = {1}", yourPoint.x, yourPoint.y);
   }
}

Output

My Point:   x = 0, y = 0
Your Point: x = 10, y = 10

Example 2

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

// keyword_struct2.cs
// Declare a struct object without "new"
using System;
public struct Point 
{
   public int x, y;

   public Point(int x, int y) 
   {
      this.x = x;
      this.y = y; 
   }
}

class MainClass 
{
   public static void Main() 
   {
      // Declare an object:
      Point myPoint;

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

      // Display results:
      Console.WriteLine("My Point:");
      Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);
   }
}

Output

My Point:
x = 10, y = 20

See Also

C# Keywords | Structs Tutorial | Default Values Table | Built-in Types Table | Types | Value Types | class | interface

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.