Design Guideline for C# Structs (C Sharp)
focuses on the design guidelines that has to be followed when creating
and using structs in your C# code. Basically structures can do most of
what classes do. However there are certain differences between both. To
begin with, you have to know when you are recommended to use structs instead
Click Here To Try It FREE for 7 days
When to Use Structures?
the instances are relatively small
Design Guidelines for Using Structures:
Logically speaking, entire structure you design should represent one value.
Size of the instance should be less than 16 bytes.
The structure you design should be immutable.
Structures cannot be inherited.
Structures cannot be declared as static.
When an instance of structure is passed as an argument to a method, it is pass by value and not pass by reference.
One big difference between structs and classes is that structs is a value type and classes are reference types. Since structs are value types, allocating and deallocating them is not as expensive as it requires for reference types. However, assigning a structure to an object and vice versa requires boxing and unboxing to happen. This is a performance overhead. Hence use structures if and only if your requirement requires relatively less number of boxing and unboxing to happen.
You should not explicitly define a default constructor for structure. C# compiler doesnt allow you to do it. This is because during runtime, a constructor will run internally and all elements of the structure will be assigned with the default value. Consider the following code:
In this code, you have defined two constructors, one constructor with parameters and the other without constructors. Structures accept constructors with parameters. But the constructor without parameter is the default constructor and is not permissible. Hence the above code will end up in the compilation error Structs cannot contain explicit parameterless constructors
of the structures cannot be directly initialized. Consider the following
IEquatable function on structures wherever appropriate. IEquatable function
is available in System namespace. It is used to compare two value types
and determine if they are equal. It does the same job as that of equals
method. Since structure is a value type, you can use this IEquatable function
to compare structures as well. The interface in IEquatable will eliminate
the need for boxing and reflection when compared to equals
Output of this example will be:
Strings are Equal
In this example,
you have an assumption that the member is always initialized using the
constructor defined above. Hence in compareString method, you performed
null check for the parameter alone. What if you create an instance of
the structure without calling the constructor above? Then member value
will be null and you perform equals function on a null value thereby ending
into a runtime exception. Modify the class testClass as shown below:
this problem, modify compareString function as shown below:
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
| Design Guideline for C# Structs (C Sharp) | Design Patterns Its Importance and Types | How to Implement Proxy Pattern Using C# (C Sharp)? | How to Implement Singleton Pattern Using C# (C Sharp)? | Illustration of Abstract Classes of C# (C Sharp) with Examples | Illustration of Sealed Classes of C# (C Sharp) with Examples | List of Overloadable Operators in C# (C Sharp) | Usage of [ ] and () Operators in C# (C Sharp) | What is C# (C Sharp) Nested Type? |