Monday, April 23, 2007

What is the difference between a Struct and a Class?

classes are reference types stored in the heap,
structs are value types stored on the stack

structs don’t support inheritance
classes  support inheritance


You will tend to use structs for smaller data types for performance reasons

you could intialize structs without the new keword
 ... but in classes  u have to 

public struct Point {

public int x, y;

public Point(int p1, int p2){
x = p1;
y = p2;
                                               }
                                   }


in other place u could use

Point myPoint = new Point();
Point yourPoint = new Point(10,10);

and use

// Declare an object:
Point myPoint;

// Initialize:
myPoint.x = 10;
myPoint.y = 20;


                


0 Comments:

Post a Comment

<< Home