Creating Structs

C# also supports structs, which you create with the struct keyword. Structs in C# are like lightweight versions of classes. They're not reference types; they're value types, so when you pass them to methods, they're passed by value. They're like classes in many ways—they support constructors, for example (but not inheritance). They take up fewer resources in memory, so when you've got a small, frequently used class, give some thought to using a struct instead. Here's how you declare a struct:

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

Here are the parts of this statement:

  • attributes (Optional)— Hold additional declarative information, as we'll see in Chapter 14.

  • modifiers (Optional)— The allowed modifiers ...

Get Microsoft® Visual C#® .NET 2003 Kick Start now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.