Re: Structs vs. Classes

Tech-Archive recommends: Speed Up your PC by fixing your registry



Tariq Karim <TariqKarim@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
The main difference between class and struct are as follows:

a) Classes are created on heap while structs are created on stack.

Class instances are indeed always created on the heap, at least with
C#, with the exception of fixed arrays which can be created on the
stack in unsafe code.

However, value type instances are *often* on the heap - as part of
other instances. If I do:

class Foo
{
int i;
}

Foo f = new Foo();

The object referred to by f is on the heap, including its "i" member.
See http://pobox.com/~skeet/csharp/memory.html for more on this.

b) Classes are passed by reference (by default) while structs are passed by
value.

No. See http://pobox.com/~skeet/csharp/parameters.html

v) Structs does not support inheritance.

They support interface inheritance, but not implementation inheritance.

Having said these differences, I belive structs is nothing but a data
structure that is exchanged and manipulated by various application elements
but structs itself does not have have any behaviour.

No...

I know structs could have method/ functions but theoritically
speaking, structs should "NOT" have any behavior rather it is a data
structure that holds data and is manipulated by various classes.

Just because they don't have behaviour in *some* languages doesn't mean
they shouldn't have behaviour in C# or .NET. Why should something being
a value type mean that it shouldn't have behaviour? Would you rather
DateTime didn't have any methods in it? That you couldn't use
int.ToString()?

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: Difference between nullable class and nullable<> structure
    ... The heap, in general, is open memory, while the stack is program memory. ... Now, anyone who has programmed for a good length of time will note that this statement is not absolutely 100% correct and possibly even misleading, but it is very useful when explaining why a person who writes only structures and no classes is more apt to end up with an out of stack memory condition in his program because he heard that structures are faster. ... For a program using exclusive structs as the user-defined types to have the persistent instances of those structs allocated on the stack, you'd have to make a new method call for each object you want to allocate, and that method would have to not return until you no longer need that object. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Stack vs. Heap Question, Please Help
    ... on the heap (along with the concept of stack frames) is very important ... they access the same object on the heap, ... are any structs they contain directly. ... You don't need to use the words stack or heap to explain this. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Please Explain where will the struct be stored if it is declared inside the Class
    ... forget about structs for a second. ... can be stored either on the stack, or on the heap. ... First, think about the stack. ... A struct would act exactly the same as any of these decimals and ints. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Struct vs. Class
    ... structs can provide performance ... > an array, or boxed, they're on the heap. ... they are only completely allocated on the stack if they are ... 0x0012f600 00aa2f00 - this is a reference to the string type on the GC ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Structs vs. Classes
    ... Classes are created on heap while structs are created on stack. ... different from "Bob Smith, customer #1234". ... reference to that customer in your entire program see that change. ...
    (microsoft.public.dotnet.languages.csharp)