JavaScript Is Best Script
- Joined
- Dec 13, 2010
- Messages
- 631
- Reaction score
- 131
According to MSDN, "Each is essentially a data structure that encapsulates a set of data and behaviors that belong together as a logical unit. A class or struct declaration is like a blueprint that is used to create instances or objects at run time"
But what is the difference between a struct and a class? The main difference is that a struct is generally a value type while a class is generally a reference type. But what does this means anyway?
Class - Reference type :
Struct - Value type :
So to sum things up a bit, a struct is a value type and it copies itself whenever it gets passed around. Classes on the other hand are reference types and they themselves (to be more specific : a pointer pointing to them) get passed around instead of copying themselves and passing their copies around.
So when do we use structs, when do we use classes?
Performance-wise, as you can see that structs make a copy of themselves whenever they are passed around, you generally do not, will not and shall not use structs for extremely large data types. Why? Lets say you have an instance of a struct that uses up 2 MB of memory. Whenever you pass it around your computer has to copy exactly these 2MB and pass this copy around. This means that 1) Your program will eat up a lot of memory, 2) Your program might take a bit long to copy this memory. Hence, Structs are used for small data types while classes for large(to be specific : more complex) data types.
Some examples of structs in the .NET Framework : byte, int, short, ushort, BigInteger, long, double
Some examples of classes in the .NET Framework : FileStream, Socket, TcpListener
You might have noticed in my first paragraph i said that classes are generally reference types. However there are exceptions, such as the string class which is a value type.
Well that's all for now.
But what is the difference between a struct and a class? The main difference is that a struct is generally a value type while a class is generally a reference type. But what does this means anyway?
Class - Reference type :
Whenever you pass an instance of a class around, you are actually passing itself around, just as how you do in real life. For example, you create an apple and you want your dad to have it. You simply pass that apple to your dad. Whatever he does to the apple (eat it, paint it, slice it), the changes will be reflected directly on the apple you created. Pretty straight forward lets see some examples:
So yeah, that's what a reference type is. Any changes is reflected upon the original object itself. Heres a declaration of a class:
Code:
Apple myApple = new Apple(); // Assume we have a [B]class[/B] named "Apple"
myApple.sweetness = 0; // Sets a field to 0
Apple dadApple = myApple; // Passes my apple to dad
dadApple.sweetness += 2; // Adds 2 to the field of dad's apple, which is also my apple
Console.WriteLine(myApple.Sweetness + "," + dadApple.Sweetness); // Outputs "2,2"
Code:
class Apple
{
public int sweetness; // a field in the struct
public void ChopIntoHalf() // some method in the struct
{
this.sweetness /= 2;
}
}
Struct - Value type :
Whenever you pass an instance of a struct around, you are actually passing a copy of it around. What does this means, well it means for example, you create an apple A and you want to let your dad have possession of this apple. To do so, since your apple is a struct type, you would create an exact copy B of apple A and pass B to your dad. Whatever your dad does to the apple (eat it, paint it, slice it) will NOT affect apple A. That's what a struct is. Whenever you pass it around as a parameter, you are passing the VALUE of it, not the object itself. Any changes to the copy will NOT affect the original copy. Let's take an example :
As you can see, My apple doesn't get affected by the changes done to dad's apple, because i copied my apple initially and assigned this copy to dad to make it his. Therefore the apple dad has is merely a copy of mine. Here's an example of a struct:
As you can see, its same as declaring a class. however it is important to note that some rules are different which i left out (damn).
Code:
Apple myApple = new Apple(); // Assume "Apple" is a struct
myApple.sweetness = 0; // sets a field to 0
Apple dadApple = myApple; // [B]Copies[/B] my apple and passes the copy to dad to make it his
dadApple.sweetness += 2; // Adds 2 to dad's apple, which is the [B]copy[/B] of my apple
Console.WriteLine(myApple.Sweetness + "," + dadApple.Sweetness); // Outputs "0,2"
Code:
struct Apple
{
public int sweetness; // a field in the struct
public void ChopIntoHalf() // some method in the struct
{
this.sweetness /= 2;
}
}
So to sum things up a bit, a struct is a value type and it copies itself whenever it gets passed around. Classes on the other hand are reference types and they themselves (to be more specific : a pointer pointing to them) get passed around instead of copying themselves and passing their copies around.
So when do we use structs, when do we use classes?
Performance-wise, as you can see that structs make a copy of themselves whenever they are passed around, you generally do not, will not and shall not use structs for extremely large data types. Why? Lets say you have an instance of a struct that uses up 2 MB of memory. Whenever you pass it around your computer has to copy exactly these 2MB and pass this copy around. This means that 1) Your program will eat up a lot of memory, 2) Your program might take a bit long to copy this memory. Hence, Structs are used for small data types while classes for large(to be specific : more complex) data types.
Some examples of structs in the .NET Framework : byte, int, short, ushort, BigInteger, long, double
Some examples of classes in the .NET Framework : FileStream, Socket, TcpListener
You might have noticed in my first paragraph i said that classes are generally reference types. However there are exceptions, such as the string class which is a value type.
Well that's all for now.
Last edited: