Structures
In C#, a structure is a value type data type. It helps you to make a single variable to hold the related data of various data types. Structures are used to represent a record.
Features of Structures
Structure members cannot be specified as abstract, virtual, or protected.
- Structures can have methods, fields, indexers, properties, operator methods and events.
- Structures can have defined constructors, but not destructors. However, you cannot define a default constructor for a structure. The default constructor is automatically defined and cannot be changed.
- Unlike classes, structures cannot inherit other structures or classes.
- Structures cannot be used as a base for other structures or classes.
- A structure can implement one or more interfaces.
- When you create a struct object using the New operator, it is created and the appropriate constructor is called. Unlike classes, structs can be instantiated without using the new operator.
- If the new operator is not used, the fields remain unassigned and the object cannot be used until all the fields are initialized.
- public struct Struct_Emp
- {
- // Fields
- private string Empname;
- private int Empid;
- // Property
- public string Empname
- {
- get
- {
- return Empname;
- }
- set
- {
- Empname = value;
- }
- }
- // Method
- public int GetEmpid()
- {
- return Empid;
- }
- }
Class Vs Structures
- classes are reference types and structs are value types
- structures do not support inheritance
- structures cannot have default constructor
When to use Structures
- When you want your type to look and feel like a primitive type.
- When you create many instances, use them briefly and then drop them. For for example, within a loop.
- The instances you create are not passed around a lot.
- When you don't want to derive from other types or let others derive from your type.
- When you want others to operate on a copy of your data (basically pass by value semantics).
When not to use Structures
The size of the struct (the sum of the sizes of its members) is large
- You create instances, put them in a collection, iterate and modify elements in the collection. This will result in many boxing/unboxing since FCL Collections operate on the system.

Banketeshvar NarayanPosted Nov 18, 2015, 12:34 PM
Nice
Shridhar SharmaPosted Nov 17, 2015, 1:07 PM
nice share
Nishant MittalPosted Nov 17, 2015, 10:03 AM
Good Info ..Indeed we forget about Structures :)
Upendra Pratap ShahiPosted Nov 17, 2015, 9:43 AM
good...thanks for sharing..
Sibeesh VenuPosted Nov 17, 2015, 3:58 AM
Nice Share
Ankit BansalPosted Nov 17, 2015, 12:44 AM
nice one..
Rajeesh MenothPosted Nov 17, 2015, 12:14 AM
Thanks for sharing..
Humayun Kabir MamunPosted Nov 17, 2015, 12:10 AM
Nice...
Former memberPosted Nov 16, 2015, 1:51 PM
Maybe class, maybe struct. As a rule of thumb: If the object is :1. Small 2. Logically an immutable value 3. There's a lot of them Then I'd consider making it a struct. Otherwise I'd stick with a reference type
Raja TPosted Nov 16, 2015, 11:40 AM
Nice one, Thanks for sharing...
Anil SinghPosted Apr 7, 2015, 12:44 AM
Awesome post!! Thank you! http://www.code-sample.com/
Gowtham RajamanickamPosted Apr 6, 2015, 10:01 AM
fantastic..
Gowtham RajamanickamPosted Apr 6, 2015, 10:01 AM
great..
Vineet KumarPosted Apr 6, 2015, 6:36 AM
Good start. Welcome to cSharpCorner :)
Former memberPosted Apr 6, 2015, 5:54 AM
Good one
Santhakumar MunuswamyPosted Apr 6, 2015, 4:13 AM
Good