This post is to discuss about the structure in C#. A structure has following characteristics.
- Structure is value type which is used to encapsulate small group of related variables.
- Data stored on stack.
- When one copy from one structure it copies all the data. So if any changes in one structure does not affect another.
- Structure cannot support inheritance.
- Structure can also implement interface.
- Field initialization cannot be done within structure unless they are constant or read only.
- Structure cannot have default constructor and destructor.
- Structure can initialize without using new operator. You can also new operator to initialize structure.
- Structure cannot be declare static, but it contain static member.
- Structure cannot be declare static, sealed, abstract.
- Structure can be used as nullable type and can be assigned null value.
- Used for lightweight object and small data structure.
- Default member accessibility of the structure is Private.
- Default accessibility of structure is internal is no access modifier used.
STRUCTURE INITIALIZATION
Structure can be initialized with or without using new operator.
In the above example, we initialize the structure without using new operator.
STRUCTURE INITIALIZATION WITH PARAMETER CONSTRUCTOR
Structure cannot have default constructor or parameterless constructor. But it can have constructor with arguments.
- Structure is value type which is used to encapsulate small group of related variables.
- Data stored on stack.
- When one copy from one structure it copies all the data. So if any changes in one structure does not affect another.
- Structure cannot support inheritance.
- Structure can also implement interface.
- Field initialization cannot be done within structure unless they are constant or read only.
- Structure cannot have default constructor and destructor.
- Structure can initialize without using new operator. You can also new operator to initialize structure.
- Structure cannot be declare static, but it contain static member.
- Structure cannot be declare static, sealed, abstract.
- Structure can be used as nullable type and can be assigned null value.
- Used for lightweight object and small data structure.
- Default member accessibility of the structure is Private.
- Default accessibility of structure is internal is no access modifier used.
STRUCTURE INITIALIZATION
Structure can be initialized with or without using new operator.
public struct Customer
{
public string Name;
public string Address;
}
public static void Main()
{
Customer newCustomer;
newCustomer.Name = "Praba";
newCustomer.Address = "India";
}
In the above example, we initialize the structure without using new operator.
STRUCTURE INITIALIZATION WITH PARAMETER CONSTRUCTOR
Structure cannot have default constructor or parameterless constructor. But it can have constructor with arguments.
public struct Customer
{
public string Name;
public string Address;
public Customer(string name, string address)
{
Name = name;
Address = address;
}
}
public static void Main()
{
Customer newCustomer = new Customer("Praba","India");
}
No comments:
Post a Comment