Showing posts with label Struct. Show all posts
Showing posts with label Struct. Show all posts

Wednesday, May 29, 2013

Class Vs Structs

Both Classes and Structs are constructs in C# which are used to create custom complex types by encapsulating a set of basic types and logic. Though they are used to create custom types there are some underlying differences between the two, the following are some of the differences between classes and structs.

Sunday, May 26, 2013

What is a Struct?

Structs can be used to define custom types, which are build using a combination of the primitive types like int, string etc. For example we can define an Employee Struct which can hold details of the employee id, name, address etc in individual types like int, string etc.

Structs are value types while classes are reference types, which means that every instance of the Struct is completely independent and have their own set of values. If a struct instance A is assigned to struct instance B then both A & B have their own memory and have an independent set of value, hence any change to instance A will not affect instance B.

The below example defines a simple Struct which contains an integer id and a string name representing an employee details.