i dont fully understand when is it more effective to use a struct and when is it more effective to use a class?


i read the explanation from Oriely's book "C# in a nutshell", but i still dont understand.

Because a struct is a value type, each instance doesnt require instantiation of an object on the heap, this incures a useful savings when creating many instances of a type.

For instance, creating an array of value type requires a single heap allocation.

A class is a reference type. When an object of the class is created, the variable to which the object is assigned holds only a reference to that memory. When the object reference is assigned to a new variable, the new variable refers to the original object. Changes made through one variable are reflected in the other variable because they both refer to the same data.

A struct is a value type. When a struct is created, the variable to which the struct is assigned holds the struct's actual data. When the struct is assigned to a new variable, it is copied. The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

In general, classes are used to model more complex behavior, or data that is intended to be modified after a class object is created. Structs are best suited for small data structures that contain primarily data that is not intended to be modified after the struct is created.

Thanks Mitja. it is clear to me now.
could you also tell me why adding to a struct variable makes it a mistake?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CPoint[] cPoints = new CPoint[10];

            Point[] points = new Point[10];
           
            //cPoints[7].X=20; //runtime error

            points[7].X=20;
            Console.WriteLine(points[7].X);


        }
    }

}
class CPoint
{
    public int X ;
}
struct Point
{
    public int X;
}

You did a mistake in your last statement, I thnik you had this in mind: "adding to a CLASS variable makes it a mistake".

Answer: (read my post carefully ones again from the beginning). Its because your variable "cPoints" it is only a reference to a class.


You have to do it like this:

namespace Jan11Struct
{
    class Program
    {
        static void Main(string[] args)
        {
            CPoint[] cPoints = new CPoint[10];
            SPoint[] sPoints = new SPoint[10];

            //cPoints[7].X = 20; //runtime error
            cPoints[7] = new CPoint(20);
            sPoints[7].X = 20;
            Console.WriteLine("Class value: {0} .... Struct value: {1}.", cPoints[7], sPoints[7].X);
            Console.ReadLine();
        }
    }
}

class CPoint
{
    public int X;
    public CPoint(int _X)
    {
        this.X = _X;
    }
}
struct SPoint
{
    public int X;
}

Hope this helps explaning the difference between the class and the struct.
Mitja

Comments
Now i understand

i see , Mitja..

The new variable and the original variable therefore contain two separate copies of the same data. Changes made to one copy do not affect the other copy.

could you try to demonstrate to me that statement, cause i am not sure what it means

We are talking about structs now and your example, this is what you are asking:

SPoint sPoint1 = new SPoint();
 sPoint1.X = 999;
 SPoint sPoint2 = sPoint1;

i am still unsure why to use a struct instead of a class, why a struct limits me.

and if the class gives me all what i want. why not to neglect the struct and never use it?

This question has already been answered. Start a new discussion instead.