Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

What about Structs?
By Dino Karatzios

A new feature that was introduced in the C# language is the STRUCT object. To give a general and easy definition of STRUCTS, you can think of them as something like a scaled down class. One important aspect of STRUCTS is that they were designed for situations where you simply wanted to group data that is relative.

Example of Class vs. Struct ’
class  Teacher
{
 private string Name;
 private string Address;
 private string City;
 private long Phone;
}
struct Teacher
{
 private string Name;
 private string Address;
 private string City;
 private long Phone;
}

So, why use a struct and not a class object? Before answering that question, we should look at the significant differences between both objects. First and foremost, a STRUCT is a value-type object, while class is a reference type object. If you are not familiar with value-type and reference type objects, this simply means that the STRUCT object is allocated (stored) in a location of memory called the stack, and the class object is allocated (stored) in another section of memory called the heap.

Allocating memory for a STRUCT object on the stack does have it’s advantage. Because it is on the stack, retrieving data from the STRUCT’s members is very fast, as is removing them from the stack. The disadvantage however, is that when a struct object is assigned to another struct object (see below), the full content of the struct that is being copied is duplicated and added to the stack, resulting in a lost efficiency known as a performance hit. When using a reference type object however, only a reference to the class is added to the stack.

Example using a class

public void SomeMethodB()
{                     
 Teacher A = new Teacher();
 Teacher B = A;
}

* Note: class A and class B are both 
referencing the same object on the heap.
Only a new reference has been added to
the stack.
Example using a struct

public void SomeMethodA()
{
 Teacher A = new Teacher();
 Teacher B = A;
}

* Note: struct A and struct B are both allocated
in the stack even though they contain the same
members.

Structs and Inheritance?

The second significant difference between class objects and struct objects is about inheritance. Let us cut the chase and get right to the point, structs do no support inheritance. This means that it is not possible to inherit a struct , nor can a struk be derived from.

Structs and Constructors?

The third significant difference between class objects and struct objects refers to constructors. Constructors are supported by structs in the C# language, and you can define constructors for structs in exactly the same manner as you would for classes. If you are not familiar with constructors, they are methods with the same name as the struct or class ( see figure below). However, the only exception with struct constructors is that you are not permitted to define a constructor that takes no arguments.

Example of a valid constructor

using System;

struct Teacher
{
 private string Name;
 private string Address;
 private string City;
 private long Phone;

 //Structs' Constructor
 public Teacher(string name, string address, string city, string phone) 
 {
  Name = name;
  Address = address;
  City = city;
  Phone = phone;
 }
}
An example of a non-valid Constructor

using System;

struct Teacher
{
 private string Name;
 private string Address;
 private string City;
 private long Phone;

//Structs\’ Constructor is not valid because there are no
//arguments defined. A constructor with no arguments is not permitted!.
 public Teacher() 
 {
  Name = "Bob" ;
  Address = "50 King St.";
  City = "Saint John";
  Phone = 643-2333;
 }
}
So why is a struct constructor with no arguments not permitted in C#? This is because structs already contain a default constructor which has no arguments. Keep in mind that this default constructor is part of the .Net framework, therefore it is not visible in our code. The sole purpose of the default constructor is to assign default values to its members. For example, observe the following code,
using System;

struct Grade
{
 int x;
 int y;

 public ShowValues()
 {
  Console.WriteLine("x is {0}", x); 
  Console.WriteLine("y is {0}",y);
 } 
}

class Test
{
 public static void Main()
 {
  //When we instantiated the struct Grade using the new operator,
  //the default constructor will run and assign the values 0 for x
  //and 0 for y. This is why the answer will be:
  //x is 0
  //y is 0
  Grade g = new Grade();
  g.ShowValues()
 }
}
Structs and Instantiation

The last significant difference between the class object and the struct object refers to instantiation. Until now, the syntax for instantiating structs is the same as instantiating a class, however there is something different happening behind the scenes. The "new" operator does not work in the same way as it does for classes and other reference type objects. Instead of allocating memory, the "new" operator simply calls the default constructor. Therefore, it is possible to modify the previous example with the following code:

Grade g;
g.ShowValues();
But there is one drawback, a struct is only fully initialized when values have been assigned to ALL of its fields. For that reason, the code "Grade g; " does not fully initialize the struct Grade and the following code "g.ShowValues" will not compile.

NON- Valid initialization

class Test
{
 public static void Main()
 {
  Grade g;
  g.ShowValues()
 }
}
Valid Initialization

class Test
{
 Public static void Main()
 {
  Grade g;
  g.x=0;
  g.y=0;
  g.ShowValues()
 }
}

Conclusion

In conclusion, we can observe from our previous topics that Structs can be valuable objects and can increase your application performance. However, you should keep in mind that structs are usually small in size relative to classes when used in an application. Structs are value types objects, and have no inheritance functionality. Hence the reason why they are used seldom relative to classes.