Quote:
Originally Posted by shadowman123
Correct me Guyz if iam Wrong ...i Seen how Struct Work and i've seen a comparison bet Class , Struct So the conclusion is that Class is faster then struct in usage so it not good to work with structs but what make me Confused how can i use struct if class is more efficient than it ??
|
The main thing to point out is that classes are "reference" types while structs are "value" types. If you don't know the difference between the two start reading

. Another major aspect is the fact that structs do not support inheritance while classes do. (Again read if you don't understand this) Here is a simple C# program to show you difference between class "reference" and struct "value".
Code:
using System;
namespace DifferenceBetweenClassesAndStructures
{
struct StrucutreExample
{
public int x;
}
class ClassExample
{
public int x;
}
class Program
{
static void Main(string[] args)
{
StrucutreExample st1 = new StrucutreExample(); // Not necessary, could have done StructureExample1 st1;
StrucutreExample st2;
ClassExample cl1 = new ClassExample();
ClassExample cl2;
cl1.x = 100;
st1.x = 100;
cl2 = cl1;
st2 = st1;
cl2.x = 50;
st2.x = 50;
Console.WriteLine("st1 - {0}, st2 - {1}", st1.x, st2.x);
Console.WriteLine("cl1 - {0}, cl2 - {1}", cl1.x, cl2.x);
Console.ReadLine();
}
}
}
EDIT: Didn't even answer your question...
Well, unlike classes, structs are created on stack. So, it is faster to instantiate and destroy a struct as opposed to a class. I'd recommend you Google this information for more details.