Struc Vsclass
Struc Vsclass
Struc Vsclass
Since classes are reference type, a class variable can be assigned null.But we cannot assign null to
struct AStruct
{
int aField;
}
class AClass
{
int aField;
}
class MainClass
{
public static void Main()
{
AClass b = null; // No error.
AStruct s = null; // Error [ Cannot convert null to 'AStruct'
because it is a value type ].
}
}
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 ).
5. You cannot have instance Field initializers in structs.But classes can have
initializers.
class MyClass
{
int myVar =10; // no syntax error.
public void MyFun( )
{
// statements
}
}
struct MyStruct
{
int myVar = 10; // syntax error.
public void MyFun( )
{
// statements
}
}
6. Classes can have explicit parameterless constructors. But structs cannot have
class MyClass
{
int myVar = 10;
public MyClass( ) // no syntax error.
{
// statements
}
}
struct MyStruct
{
int myVar;
public MyStruct( ) // syntax error.
{
// statements
}
7. Classes must be instantiated using the new operator. But structs can be
MyClass aClassObj; // MyClass aClassObj=new MyClass(); is the correct
format.
aClassObj.myVar=100;//NullReferenceException(because aClassObj does not contain a
reference to an object of type myClass).
MyStruct aStructObj;
aStructObj.myVar=100; // no exception.
face="verdana"size="2">
8. Classes support inheritance.But there is no inheritance for structs.
( structs don't support inheritance polymorphism )
(1)
struct MyStruct
{
int aStructVar;
internal void aStructMethod()
{
// statements
}
}
class MyClass : MyStruct // Syntax error.
{
int aClassVar;
int aClassMethod()
{
// statements
}
}
(2)
class MyClass
{
int aClassVar;
int aClassMethod()
{
// statements
}
}
struct MyStruct : MyClass // Syntax error.
{
int aStructVar;
internal void aStructMethod()
{
// statements
}
}
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.
class MyClass //No error( No matter whether the Field ' MyClass.myString ' is
initialized or not ).
{
int myInt;
string myString;
public MyClass( int aInt )
{
myInt = aInt;
}
}
struct MyStruct // Error ( Field ' MyStruct.myString ' must be fully assigned
before it leaves the constructor ).
{
int myInt;
string myString;
public MyStruct( int aInt )
{
myInt = aInt;
}
}
struct MyStruct
{
int myInt;
public MyStruct( )
{
}
~MyStruct( ) //Error.
{
Console.WriteLine("Destructor of MyStruct object");
}
}
class MyClass
{
int myInt;
public MyClass( )
{
}
~MyClass( ) // No Error.
{
Console.WriteLine("Destructor of MyClass object");
}
}
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.
A Sample Code:
using System;
class MainClass
{
static void Main( )
{
Console.WriteLine( "Struct \n" );
MyStruct var1=new MyStruct( );
var1.Member="Initialized";
// value of var1 is assigned to var2
MyStruct var2=var1;// stmt > [A]
//var1.str & var2.str are different memory locations in the stack.
Console.WriteLine(var1.Member+"\n"+var2.Member);
Console.WriteLine( "" );
var1.Member="Assigned";
Console.WriteLine( var1.Member+"\n"+var2.Member );
Console.WriteLine( "\nClass\n" );
MyClass obj1=new MyClass( );
obj1.Member="Initialized";
// reference(or simply address)of an object stored in obj1 is assigned to
obj2.
MyClass obj2=obj1;// stmt > [B]
//obj1 & obj2 are 2 reference variables in the stack.They points to a
single object
//in the heap.So obj1.str & obj2.str represent same memory location in the
heap.
Console.WriteLine( obj1.Member+"\n"+obj2.Member );
Console.WriteLine( "" );
obj1.Member="Assigned";
Console.WriteLine( obj1.Member+"\n"+obj2.Member );
}
}
public struct MyStruct
{
string str;
public string Member
{
get
{
return str;
}
set
{
str=value;
}
}
}
public class MyClass
{
string str;
public string Member
{
get
{
return str;
}
set
{
str=value;
}
}
}
Output:
Struct
Initialized
Initailized
Assigned
Initailized
Class
Initialized
Initailized
Assigned
Assigned