Tutorials
Desktop Programming
C Sharp
STRUCTS IN C#
Written by Christopher Thursday, 29 May 2008 15:51
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.

yuvraj g. tikam
said:
|
... 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. |
|
Votes: +0
| Training Tip #1 :: Push-ups on the Swiss Ball admin 28.4.2009 7:07 |
Re:hello admin 21.4.2008 14:11 |
| Dell Inspiron 1545 Windows XP Drivers admin 12.4.2009 8:03 |
hello yuppy 21.4.2008 10:28 |
| Re:Get rich ebooks - are they really working? lann 23.4.2008 17:04 |
Re:Get rich ebooks - are they really working? admin 19.4.2008 14:08 |