C# Structs


A new feature that was introduced in the C#language is the STRUCT object. To give a general and easy definition ofSTRUCTS, you can think of them as something like a scaled down class.One important aspect of STRUCTS is that they were designed forsituations 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 significantdifferences between both objects. First and foremost, a STRUCT is avalue-type object, while class is a reference type object. If you arenot familiar with value-type and reference type objects, this simplymeans that the STRUCT object is allocated (stored) in a location ofmemory called the stack, and the class object is allocated (stored) inanother section of memory called the heap.

Allocating memory for a STRUCT object on thestack does have it?s advantage. Because it is on the stack, retrievingdata from the STRUCT?s members is very fast, as is removing them fromthe stack. The disadvantage however, is that when a struct object isassigned to another struct object (see below), the full content of thestruct that is being copied is duplicated and added to the stack,resulting in a lost efficiency known as a performance hit. When using areference type object however, only a reference to the class is addedto 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 betweenclass objects and struct objects is about inheritance. Let us cut thechase and get right to the point, structs do no support inheritance.This means that it is not possible to inherit a struct , nor can astruk be derived from.

Structs and Constructors?

The third significant difference between classobjects and struct objects refers to constructors. Constructors aresupported by structs in the C# language, and you can defineconstructors for structs in exactly the same manner as you would forclasses. If you are not familiar with constructors, they are methodswith the same name as the struct or class ( see figure below). However,the only exception with struct constructors is that you are notpermitted 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 noarguments not permitted in C#? This is because structs already containa default constructor which has no arguments. Keep in mind that thisdefault constructor is part of the .Net framework, therefore it is notvisible in our code. The sole purpose of the default constructor is toassign default values to its members. For example, observe thefollowing code,
Continues…

Pages: 1 2

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!