Understanding the differences between a class and struct in C# :
If you are not an experienced programmer, you might be confused by the classes and the structs. Assuming that you might be already a little confused by the differences between function and classes, we will try to explain in detail the structs here.
Struct: Doesn't hold a value;
Class: Holds a value
Garbage collection:
As you probably know one of the major difference between C# and C++ is how the developer manages memory. In C# the memory is easily allocated by the garbage collector. Now, however this is partly true, because when we work with structs the memory is not directly allocated unless you use reference types and these types refer to a real value that invokes the garbage collector.
The This KEYWORD, WHEN working with structs:
In the classes the "this" keyword always defines a value, it cannot be used with keywords ref and out. Consider the following example, it will produce a compiler error. So you better don't try to remember it...:
Code:
class demonclass
{
//...
public void Method(demonclass that)
{
RefParameter(ref this); // compile error
OutParameter(out this); // compile error
this = that; // compile error
}
//...
}
The right implementation when we use struct:
Code:
struct theright
{
//...
public void Reassign(theright that)
{
RefParameter(ref this); // compiles ok
OutParameter(out this); // compiles ok
this = that; // compiles ok
}
//...
}
A struct can be used even in cases with readonly values, consider the following example:
Code:
struct theright
{
public theright(int value)
{
Field = value;
}
public void Reassign(theright that)
{
RefParameter(ref this); // compiles ok
OutParameter(out this); // compiles ok
this = that; // compiles ok
}
public readonly int Field;
}
class justaclass
{
static void Main()
{
theright s = new theright(112);
Console.WriteLine(s.Field); // the output is 112
s.Reassign(new theright(24));
Console.WriteLine(s.Field); // the output is 112
}
}
Another major difference is that a class might design its objects as private and hence they might not be accesed further in the program. On the other hand the objects are always public.
The struct cannot be null:
Code:
using System;
class Nullclass
{
static void Eg(int? x)
{
if (x != null)
{
Console.WriteLine("This is OK");// ok
}
}
public static void Main()
{
}
}
If everything went right you should see nothing on your screen...but only the empty prompt when you start it without debugging:
Can the struct use destructors?
NO. Because they don't need to, since they don't use the memory allocation, garbage collectors and so on.
How a class handles inheritance and how a struct handles inheritance:
The structs are sealed, the classes are not.
The class can't be abstract. The class could be abstract(meaning that the class can be used as a parent class).
The structs cannot use the base() method. The classes can.
The structs cannot extend other structs, and the classes can extend classes.
Protected members can't be declared within structs. The classes can declare such members(objects).
a struct can't declare abstract function members, an abstract class can.
The struct can't declare abstract, virtual and sealed functions. The classes can do that...