beginner c# question

  • Thread starter Thread starter Flip
  • Start date Start date
F

Flip

I'm coming from the java world, so I'm sorry if this is a simplistic
question. I've read enough to be dangerous :< and would like a quick
overview.

In java, everything with implemenation is implemented as a class. In C#
there are not only classes but structs, enumerations. I know one is on the
heap (classes?) and one's on the stack (structs?), but more than that, why
are they broken out? It just seems to be a bit over complicated for
something I don't quite see the benefits of? Any gentle nudges in the right
direction would be appreciated. Thanks.
 
Structs exist for perfomance reasons, they are like light classes and
because they are in the stack they don't have to wait to be collected by the
GC
Regards,
Daniel Carbajal
MVP
 
Hi,
you would have to check out the difference between reference types and value
types. say you have 2 integers a and b.
doing a = b would copy the value of b into a. say you have 2 objects a and b
doing a = b would not create a new object but makes both a and b point to
the same object. you can modify the object using either a or b. so now the
object is referenced by 2 variables. when this count drops to zero garbage
collection takes place(not right after it drops to zero, it may take some
time to do the garbage collecgtion). structures are value types. classes are
reference types. this is a starting point. you will have tro read futher in
some good c# book.
regards.
Lingesh

Daniel said:
Structs exist for perfomance reasons, they are like light classes and
because they are in the stack they don't have to wait to be collected by the
GC
Regards,
Daniel Carbajal
MVP
This posting is provided "AS IS" with no warranties, and confers no rights
 
Flip, in most projects, you won't have to worry too much on where a
variable
is allocated - unless you are in a project the requires extreame
performance.
Thank you for the clarification. I was reading a bit more about it last
night as well, and the article said I won't need it in regular (beginner :>)
programs, but maybe when I start interfacing with some Win APIs I might need
it there.

Thanks again for the clarification! :>
 
Back
Top