Partager via


struct (C# Reference)

A struct type is a value type that is typically used to encapsulate small groups of related variables, such as the coordinates of a rectangle or the characteristics of an item in an inventory. The following example shows a simple struct declaration:

public struct Book
{
    public decimal price;
    public string title;
    public string author;
}

Remarks

Structs can also contain constructors, constants, fields, methods, properties, indexers, operators, events, and nested types, although if several such members are required, you should consider making your type a class instead.

For examples, see Using Structs (C# Programming Guide).

Structs can implement an interface but they cannot inherit from another struct. For that reason, struct members cannot be declared as protected.

For more information, see Structs (C# Programming Guide).

Examples

For examples and more information, see Using Structs (C# Programming Guide).

C# Language Specification

For examples, see Using Structs (C# Programming Guide).

See Also

Reference

C# Keywords

Built-In Types Table (C# Reference)

Value Types (C# Reference)

class (C# Reference)

interface (C# Reference)

Classes and Structs (C# Programming Guide)

Concepts

C# Programming Guide

Other Resources

C# Reference

Default Values Table (C# Reference)

Types (C# Reference)