CHAPTER 29

image

Struct

The struct keyword in C# is used to create value types. A struct is similar to a class in that it represents a structure with mainly field and method members. However, a struct is a value type, whereas a class is a reference type. Therefore, a struct variable directly stores the data of the struct, while a class variable only stores a reference to an object allocated in the memory.

Struct variable

Structs share most of the same syntax as classes. For example, the struct below is named Point and consists of two public fields.

struct Point{   public int x, y;} 

Given the above struct definition, a variable of the Point type can ...

Get C# Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.