Text Size

STRUCTS IN C#

(0 votes, average: 0 out of 5)

INTRODUCTION

Structs, similar to classes, are used to define various objects in your code. They are stored on the stack, rather than in the managed heap. This means that they are value-type objects and thus, when you pass a struct as a parameter in a function, a copy of the object is actually passed to that function. This can affect the performance of your code if you use structs for large objects. However, since the .Net does not bring in the managed heap, structs are more suitable for small objects of limited functionality and with no inheritance capabilities.

 

DEFINITION

Definition of structs is similar to the definition of classes. The only thing you should modify is the keyword “class” to “struct”. For example, this is a simple struct with two public fields:

 

        struct GreekHotels

        {

            public string Town;

            public string Municipality;

        }

 

Bear in mind that the new operator does not work the same way with classes. When you need to create a new struct object, the variable declaration actually stores the object in memory. The following snippet of code demonstrates this. It declares and initializes a GreekHotel object of the struct defined in the previous example:

        static void Main()

        {

            GreekHotels Alexia;

            Alexia.Town = "Thessaloniki";

 

        }

Try to change the “struct” keyword to “class” and see what happens. In this case, Alexia would contain an uninitialized reference for an object and it would produce a compilation error. Another difference with classes is that, structs do not support initial values declarations in their definition. The following example will  be succesfully compiled only with a class keyword:

        struct GreekHotels

        {

            public string Town = "Thessaloniki";

            public string Municipality;

        }

 

 

USING STRUCTS

As mentioned earlier, when you pass a struct as a parameter in a function, the object is copied. You can avoid this by passing it as a ref parameter. This way it would be passed as a class (the address of its memory will be passed, the actual object) and would result in increased performance.

Since structs derive from the System.Object, they have access to various public and protected methods of the System.Object class. Some of the most common methods include:

 

·         ToString() : Returns a string representation of the object, but provides limited        functionality in the way the data is formatted.

·         GetHashCode(): It is used in conjunction with a dictionary. Its purpose is to provide a method (that can be overridden) for choosing the object’s place in the dictionary.

·         Equals and ReferenceEquals(): Return Boolean value for determining if two objects are the same. The first method compares instances of the object for equality whether the second method checks if two references refer to the same object.

For successful implementation of these functions it is necessary to overload them in your code in order to specify how the processes will take place.

Trackback(0)
Comments (2)add comment

yuvraj g. tikam said:

0
...
can we have array as a member of struct.
for ex.
struct exam
{
pubic int [] marks= new int[6] // array of six subjet
public int total;
public double average;
}
it is not get compiled , if there is any other alternative.
pl guide.
 
January 17, 2009
Votes: +0

yuvraj said:

0
...
can struct contains arrays, how to handle them.
 
January 17, 2009
Votes: +0

Write comment

busy

Site Statistics

Stats
Total Members
: 208
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1