This article provides an introduction to Structs in C#. It gives a basic understanding on the following
- What is struct?
- When to use Struct?
- Struct Examples
- Struct is a value type.
- It is used to encapsulate small related group of variables.
- It can also contain the following
- Constructor
- Fields
- Constants
- Methods
- Properties
- Indexers
- Operators
- Events
- Nested types
Note: If you are planning to use all the above listed members then it is better to use a class. The reason is struct are mainly used for light weight objects.
4. Struct can implement interface but cannot inherit from another struct. Thus struct members cannot be declared as protected.
5. Except constants and static fields, we cannot initialize fields within the struct declaration.
6. We can declare constructors with parameters for structs.
7. All structs are inherited from System.ValueType (which inherits from System.Object).
8. Struct can be used as nullable type and can also be assigned a null value.
9. If you want to initialize the struct members then the only way is to declare a parameterized constructor and in that constructor initialize the struc members (Refer to the example 2 in this article)
How Structs are defined?
The Structs are defined by using struct keyword.
Example
Public struct TableDimension
{
//Declare Fields, constants,properties, methods ….here
}
Example 1: Creating a Struct
struct TableDimensions
{
public int tableHeight;
public int tableLength;
}
Using struct:
private void btnStruct1_Click(object sender, EventArgs e)
{
//Declare and Initialize the struct objects
TableDimensions tbDim;
tbDim.tableHeight = 10;
tbDim.tableLength = 20;
//Displaying value from the struct objects
MessageBox.Show("Table Length " + tbDim.tableLength.ToString());
}
Note:
1. In the above example1 we can see that the struct object is created without using the new operator.
2. This means that there will be no constructor call for struct if we create an object without using the new operator.
3. In this case the struct fields will remain unassigned and cannot be used until they are assigned values. If you comment the fields assignment in the above example and try to compile the code then it will result in error.
Example 2: Creating a Parameterized constructor for struct and initialized the struct members
//Declaring a struct
struct TableDimensions
{
public int tableHeight;
public int tableLength;
public TableDimensions(int height1,int length1)
{
tableHeight = height1;
tableLength = length1;
}
}
private void btnStruct1_Click(object sender, EventArgs e)
{
TableDimensions tbDim1 = new TableDimensions(30, 60);
MessageBox.Show("Table Length " + tbDim1.tableLength.ToString()
);
}
Notes:
-
In the above example we have created a struct with the parameterized constructor to initialize the structure fields
-
When we create an object of a struct using new operator, the new operator doesn’t acts like a class but it simply calls the declared constructor and initialize all the fields.
-
Parameterized constructors are the only way in structs to initialize the struct members.
Hope you liked the introduction to struct in C#…your suggestions are welcome.
Happy Learning…by Harsh Shah
Recent Comments