Wednesday, December 10, 2008

Comparision between Structures and Classes


Structures and Class Differences:
1.
Classes are reference types and structs are value types.
Since classes are reference type, a class variable can be assigned null.But we cannot assign null to a struct variable, since structs are value type.

Example:

namespace SampleStruct

{

struct Test

{

public int add(int a, int b)

{

return a + b;

}

public void Display()

{

Console.WriteLine("Hi from Structure Declaration");

Console.ReadLine();

}

}

class Program

{

static void Main(string[] args)

{

Program p = null; //No Error

Test t = null;//Error Occurred(Cannot convert null to ‘SampleStruct.Test’because it is a value type)

}

}

}

2. When you instantiate a class, it will be allocated on the heap.When you instantiate a struct, it gets created on the stack.

3. You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct ( but dealing directly with them ).

4. When passing a class to a method, it is passed by reference. When passing a struct to a method, it's passed by value instead of as a reference.

5. You cannot have instance Field initializers in structs.But classes can have initializers.

Example:

namespace ConsoleApplication9

{

class Program

{

int y = 34;

public void Show()

{

Console.WriteLine("Hi from Structure");

Console.ReadLine();

}

static void Main(string[] args)

{

Test t;

t.Show();

}

}

struct Test

{

int x = 10;//Error: 'ConsoleApplication9.Test.x': cannot have instance field initializers in structs

public void Show()

{

Console.WriteLine("Hello from structure");

Console.ReadLine();

}

}

}


6. Classes can have explicit parameterless constructors. But structs cannot have explicit parameterless constructors.

Example:

namespace ConsoleApplication9

{

class Program

{

int y = 34;

public Program()// No Error

{

Console.WriteLine("Class Constructor");

Console.ReadLine();

}

public void Show()

{

Console.WriteLine("Hi from Structure");

Console.ReadLine();

}

static void Main(string[] args)

{

Program pgm = new Program();

Test t;

t.Show();

}

}

struct Test

{

//int x = 10;

public void Show()

{

Console.WriteLine("Hello from structure");

Console.ReadLine();

}

public Test()//Error: Structs cannot contain explicit parameterless constructors

{

Console.WriteLine("Structure Destructor");

Console.ReadLine();

}

}

}


7. Classes must be instantiated using the new operator. But structs can be instantiated without using the new operator.

Example:

namespace ConsoleApplication9

{

class Program

{

int y = 34;

public Program()

{

Console.WriteLine("Class Constructor");

Console.ReadLine();

}

public void Show()

{

Console.WriteLine("Hi from Structure");

Console.ReadLine();

}

static void Main(string[] args)

{

Program pgm;

pgm.Show();//Error: Use of unassigned local variable 'pgm'

Test t;

t.Show();

}

}

struct Test

{

public void Show()

{

Console.WriteLine("Hello from structure");

Console.ReadLine();

}

}

}


8. Classes support inheritance.But there is no inheritance for structs.( structs don't support inheritance polymorphism ). So we cannot have a base structure and a derived structure.

Example:

namespace ConsoleApplication9

{

class Program:Test//Error: 'ConsoleApplication9.Program': cannot derive from sealed type

{

int y = 34;

public Program()

{

Console.WriteLine("Class Constructor");

Console.ReadLine();

}

public void Show()

{

Console.WriteLine("Hi from Structure");

Console.ReadLine();

}

static void Main(string[] args)

{

Test t;

t.Show();

}

}

struct Test:Program//Error: Type 'Program' in interface list is not an interface {

public void Show()

{

Console.WriteLine("Hello from structure");

Console.ReadLine();

}

}

}


Note : Like classes, structures can implement interfaces

9. Since struct does not support inheritance, access modifier of a member of a struct cannot be protected or protected internal.

10. It is not mandatory to initialize all Fields inside the constructor of a class.But all the Fields of a struct must be fully initialized inside the constructor.

Example:

namespace ConsoleApplication9

{

class Program

{

int y;

public Program(int y)

{

Console.WriteLine("Class Constructor");

Console.ReadLine();

}

public void Show()

{

Console.WriteLine("Hi from Structure");

Console.ReadLine();

}

static void Main(string[] args)

{

}

}

struct Test

{

int a;

public Test(int a)//Error: Field 'ConsoleApplication9.Test.a' must be fully assigned before control leaves the constructor

{

}

public void Show()

{

Console.WriteLine("Hello from structure");

Console.ReadLine();

}

}

}


11. A class is permitted to declare a destructor.But a struct is not permitted to declare a destructor.

12. classes are used for complex and large set data. structs are simple to use.
structs are useful whenever you need a type that will be used often and is mostly just a piece of data.

13. The modifiers namely abstract and sealed are not permitted in struct. But it is permitted in class.

No comments: