Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Programming Guide
Structs (C# Programming Guide)

Updated: September 2008

Structs are defined by using the struct keyword, for example:

C#
public struct PostalAddress
{
    // Fields, properties, methods and events go here...
}

Structs share most of the same syntax as classes, although structs are more limited than classes:

  • Within a struct declaration, fields cannot be initialized unless they are declared as const or static.

  • A struct may not declare a default constructor (a constructor without parameters) or a destructor.

  • Structs are copied on assignment. When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy. This is important to remember when working with collections of value types such as Dictionary<string, myStruct>.

  • Structs are value types and classes are reference types.

  • Unlike classes, structs can be instantiated without using a new operator.

  • Structs can declare constructors that have parameters.

  • A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.

  • A struct can implement interfaces.

  • A struct can be used as a nullable type and can be assigned a null value.

Date

History

Reason

September 2008

Added text regarding assignment semantics and structs.

Customer feedback.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker