In c#, structures are same as classes, but the only difference is classes are the reference types, and structures are the value types. As a value type, the structures directly contain their value, so their object or instance is stored on the stack, and structures are faster than classes.
In c#, the structures can contain fields, properties, member functions, operators, constructors, events, indexers, constants, and even other structure types.
In c#, structures can be created by using struct
keyword. Following is the declaration of structure in the c# programming language.
If you observe the above syntax, we defined a structure “users” using struct
keyword with public access modifier. Here, the public access specifier will allow the users to create objects for this structure, and inside of the body structure, we can create required fields, properties, methods, and events to use in our applications.
Following is the example of defining a structure in the c# programming language.
If you observe the above example, we defined a structure called “User” with required fields, and we can add required methods and properties based on our requirements.
In c#, structures can be instantiated with or without new
keyword. Following is the example of assigning values to the variables of structure.
To make use of fields, methods, and events of structure, then it’s mandatory to instantiate a structure with new
keyword in c# programming language.
In c#, the structures won’t allow us to declare a default constructor or a constructor without parameters. It won’t allow us to initialize fields with values unless they are declared as const or static.
Following is the example of defining a structure with parameterized constructor and initializing the constructor fields in the c# programming language.
If you observe the above example, we defined a structure called “User” with required fields and parameterized constructor.
As discussed, the structures will allow only parameterized constructors, and fields cannot be initialized unless they are declared as const or static.
Following is the example of defining a structure with a default constructor and initializing fields with values.
When we execute the above code, we will get compiler errors because we declared a structure with the default constructor (parameterless) and initialized fields without defining it as const or static.
In c#, if we create a structure with the parameterized constructor, then we must need to explicitly initialize all the fields within the constructor before the control is returned to the caller; otherwise, we will get a compile-time error.
Following is the example of defining a structure with the parameterized constructor and required fields in the c# programming language.
When you execute the above code, we will get a compile-time error because we defined name and location variables, but we assign a value to the only name variable.
As discussed, if we create a structure with the parameterized constructor, then we must need to explicitly initialize all the fields before leaving the constructor.
Now we will see how to create a structure with fields and parameterized constructors in c# programming language with example.
Following is the example of creating a structure with different fields and parameterized constructors in c# programming language with various data members and member functions.
If you observe the above example, we defined a structure (User) by including required fields, parameterized constructor, and created an instance of structure (User) with and without new
keyword to initialize or get field values based on our requirements.
When you execute the above c# program, you will get the result as shown below.
In c#, by using structures we can implement an interface, but structures cannot inherit from another structure or class.
The following are the important characteristics of structures in the c# programming language.
struct
keyword.new
keyword.The following are the difference between structures and classes in the c# programming language.
new
operator.