Structure in C#
Structure in C#
We have learned class in the previous section. Class is a reference type. C# includes a value type entity
same as class called "structure". Structs are mainly useful to hold small data values. A structure can be
defined using the struct operator. It can contain parameterized constructor, static constructor,
constants, fields, methods, properties, indexers, operators, events and nested types.
Structure Declaration
A structure is declared using struct keyword with public, private, or internal modifier. The default
modifer is internal for the struct and its members.
The following declares the simple structure that holds data for employees.
Example: Structure
struct Employee
A struct object can be created with or without the new operator, same as primitive type variables. When
you create a struct object using the new operator, an appropriate constructor is called.
struct Employee
Console.Write(emp.EmpId); // prints 0
In the above code, an object of the structure Employee is created using the new keyword. So, this calls
the default parameterless constructor that initializes all the members to their default value.
When you create a structure object without using new keyword, it does not call any constructor and so
all the members remain unassigned. So, you must assign values to each member before accessing them,
otherwise it will give a compile time error.
struct Employee
Employee emp;
emp.EmpId = 1;
Console.Write(emp.EmpId); // prints 1
Constructors in Struct
A struct cannot contain parameterless constructor. It can only contain parameterized constructors or a
static constructor. You can declare parameterized constructor to initialize struct members, as shown
below.
struct Employee
EmpId = empid;
FirstName = fname;
LastName = lname;
Console.Write(emp.EmpId); // prints 10
Please note that you must assign values to all the members of a struct in parameterized constructor,
otherwise it will give compile time error if any member remains unassigned.
A struct can include static parameterless constructor and static fields.
struct Employee
static Employee()
EmpId = empid;
FirstName = fname;
LastName = lname;
struct Employee
EmpId = empid;
FirstName = fname;
LastName = lname;
A struct can contain events to notify subscriber about some action. Consider the following example.
Example: Structure
struct Point
public int X
get
return _x;
set
_x = value;
PointChanged(_x);
public int Y
get
{
return _y;
set
_y = value;
PointChanged(_y);
The above structure contains private fields _x and _y, properties X and Y and PointChanged event to
notify if points change. Notice that we raise the PointChanged event whenever X or Y changes. The
following code handles the PointChanged event.
class Program
{
Point.StaticMethod();
p.PointChanged += StructEventHandler;
p.X = 10;
A struct is a value type so it is faster than a class object. Use struct whenever you want to just store the
data. Generally structs are good for game programming. However, it is easier to transfer a class object
than a struct. So do not use struct when you are passing data across the wire or to other classes.
Characteristics of Structure:
Structure can include constructors, constants, fields, methods, properties, indexers, operators, events &
nested types.
A structure cannot inherit another structure or class, and it cannot be the base of a class.
Struct cannot declare a default constructor or destructor. However, it can have parametrized
constructors.
Struct can be instasntiated without the new operator. However, you won't be able to use any of its
methods, events or properties if you do so.
Struct cannot