Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the difference between class and structure explain with examples
Posted
Updated 23-Jan-12 18:21pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Jan-12 0:25am    
Why? Is MSDN not enough?

--SA
koolprasad2003 24-Jan-12 0:53am    
Gooooooled it. it's a basic thing.

Hi,
In .NET, there are two categories of types, reference types and value types.

Structs are value types and classes are reference types.

The general different is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.

A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.

A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.

This has one benefit, to begin with:

-value types always contains a value
-reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment

Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:

-copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
-copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places
When you declare variables or fields, here's how the two types differ:

-variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives
-class/struct-field: value type lives inside the class, reference type lives inside the class as a pointer to somewhere in heap memory where the actual memory lives.

A short summary of each:

Classes Only:
-Can support inheritance
-Are reference types
-Have memory overhead per new instance

Structs Only:
-Cannot support inheritance
-Are value types
-Do not have a memory overhead per new instance - unless 'boxed'

Both Classes and Structs:
-Are compound data types typically used to contain a few variables that have some logical relationship
-Can contain methods and events
-Can support interfaces

Check this to get complete details with example:
StructsVsClasses[^]
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 24-Jan-12 0:36am    
Interesting. A lot of little-known facts in this article. My 5, but I need to check some of them up.
--SA
Prasad_Kulkarni 24-Jan-12 0:39am    
Thank you sir,
Structs in C#[^]
Search in code project or google will give you lot more articles you can refer.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Jan-12 0:53am    
Thank you for this link. I must say this article has some incorrect points. For explanation, please see the comment by the member Anonymous and mine. (So, I did not vote for your post.)

--SA
Hi,

1. The major difference is that all declarations inside a structure are by default public. Class is a successor of Structure. By default all the members inside the class are private.
2. Class is reference type whereas Structure is Value type.
3. Class stores in heap whereas Structure use stack.
4. classes support polymorphism, whereas structures don't
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900