Sunday, March 17, 2013

Structure in c#.net

A structure is a value type data type. when you want a single variable to hold related data of various data types , you can create a structure. "struct" is a keyword used to create a structure.

Like classes, structures contain data members which are defined with in the declaration of the structure.
However structures differ from classes and the differences are :

  • structures are value types and get stored in a stack memory where as classes are reference types and stored in heap memory.
  • structures do not support inheritance where as classes support inheritance.
  • structures do not have default constructors.
 For example if you want to maintain  the employee details, such as employee id, employee name, joining date, designation, in a single variable, you can declare a structure.

The following code shows the syntax of a structure data type:

public struct employee_details
{
public string emp_id;
public string  emp_name;
public string join_date;
public string designation;
};

No comments: