Thursday, September 25, 2008

Structures or simply struct

User-defined types are also called structs.It's a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, events. Instances of these types are stored on the stack and they contain their data directly.
Struct can be easily converted to and from a value type to a reference type by changing the struct keywords to Class. If you make that change, instance size of the struct more than 16 bytes.
While the functionality is similar, structures are usually more efficient than classes. You should define a structure, rather than a class, if the type will perform better as value type than a reference type. Specifically, structure types should meet all of these criteria:
  • Logically represents a single value.
  • Has an instance size less than 16 bytes.
  • Will not be changed after creation.
  • Will not be cast to a reference type.

No comments: