C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 306,811 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,694 people online right now. Registration is fast and FREE... Join Now!




Structs vs Classes

 

Structs vs Classes, Whats the difference?

Footsie

25 Oct, 2007 - 12:24 PM
Post #1

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 346



Thanked: 9 times
Dream Kudos: 50
My Contributions
To me structs and classes look basically the same.

Whats the difference?
When would you use one and not the other?

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Structs Vs Classes

25 Oct, 2007 - 12:41 PM
Post #2

I Code, Therefore I am
Group Icon

Joined: 26 Jul, 2007
Posts: 14,919



Thanked: 517 times
Dream Kudos: 11525
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Here are just some of the differences between a struct and a class:
  • Classes are reference types and structs are value types. Since classes are reference type, a class variable can be assigned null, but we cannot assign null to a struct variable, since structs are value type.
  • You will always be dealing with reference to an object ( instance ) of a class. But you will not be dealing with references to an instance of a struct, you will be dealing directly with struct
  • Classes must be instantiated using the new operator, but structs can be instantiated without using the new operator.
  • Classes support inheritance.But there is no inheritance for structs, (structs don't support inheritance polymorphism )
  • It is not mandatory to initialize all Fields inside the constructor of a class. But all the Fields of a struct must be fully initialized inside the constructor.
  • structs object is allocated in the stack, and the class object is allocated in the heap.
Class are usually for large amounts of data, whereas structs are smaller, and often used when you want to group similar data together. Use a class when object identity is more important than value. Use a struct when the value contained by an instance is more important than instance identity.

Struct variables directly contain their values, so when you pass a struct instance as a parameter, it can be more expensive than passing an instance of a reference type, due to
the copying costs.

This post has been edited by PsychoCoder: 25 Oct, 2007 - 12:43 PM
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Structs Vs Classes

25 Oct, 2007 - 03:39 PM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,347



Thanked: 410 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(Footsie @ 25 Oct, 2007 - 04:24 PM) *
When would you use one and not the other?


Structs exist for one, and only one, reason: to make C coders feel all warm and fuzzy. ( ref exists for probably the same reason )

The only advantage a struct offers is a functionally different memory allocation model. Should an OO programmer running managed code care? No, they shouldn't have to. However, in C a struct is really THE record type and it's used quite extensively in the Win32 API, among others.

The only possible reason to use a struct is if you have to hook into some legacy DLL. For C# programming, the things are an ugly appendix leaving confusion and crufty code in their wake.

Can you tell I don't like struct? Seriously, there are no good reasons to use them pure C# architecture. The only one you'll deal with frequently in regular C# is DateTime, and that's caused programmers more pain and suffering than daylight savings time itself. wink2.gif

User is offlineProfile CardPM
+Quote Post

Binary_Ninja

RE: Structs Vs Classes

25 Oct, 2007 - 05:40 PM
Post #4

New D.I.C Head
*

Joined: 24 Oct, 2007
Posts: 48


My Contributions
Also,a structure cant have a parameterless constructor, and a structure is not allowed to have a destructor.
User is offlineProfile CardPM
+Quote Post

sontek

RE: Structs Vs Classes

25 Oct, 2007 - 06:21 PM
Post #5

D.I.C Regular
Group Icon

Joined: 13 Sep, 2001
Posts: 283



Thanked: 4 times
Dream Kudos: 85
My Contributions
Structs are good for small data objects that deal with mathematics, like complex numbers and points in a coordinate .

But as everyone said, the biggest difference is that structs are stored on the stack and classes are on the heap. Structs also do not support any inheritance.
User is offlineProfile CardPM
+Quote Post

Footsie

RE: Structs Vs Classes

26 Oct, 2007 - 12:38 AM
Post #6

D.I.C Regular
Group Icon

Joined: 20 Sep, 2007
Posts: 346



Thanked: 9 times
Dream Kudos: 50
My Contributions
Ok, so if I'm catching your drifts ~ in C# use classes instead and avoid structs like the plague (quote: baavgai) unless you really really have to use them?
Or unless you need to force someone to initialize certain fields?

Isn't there a way to force someone to initialize all fields in a class?
User is offlineProfile CardPM
+Quote Post

garima

RE: Structs Vs Classes

26 Oct, 2007 - 01:46 AM
Post #7

New D.I.C Head
*

Joined: 9 Oct, 2007
Posts: 46


My Contributions
QUOTE(Footsie @ 26 Oct, 2007 - 01:38 AM) *

Ok, so if I'm catching your drifts ~ in C# use classes instead and avoid structs like the plague (quote: baavgai) unless you really really have to use them?
Or unless you need to force someone to initialize certain fields?

Isn't there a way to force someone to initialize all fields in a class?

A structure would be collection of related data, while a class would be collection of data and code which handles that data.
User is offlineProfile CardPM
+Quote Post

gogole

RE: Structs Vs Classes

28 Oct, 2007 - 05:01 AM
Post #8

D.I.C Head
Group Icon

Joined: 17 Jul, 2007
Posts: 131



Thanked: 1 times
Dream Kudos: 25
My Contributions
as said earlier , structs are stored on the stack while classes are stored on the heap.they (structs) also do not support inheritance(also said earlier).i don't agree with Baavgai saying structs should be avoided,infact in some cases of programming you are better of with structs than classes for example when displaying coordinates and doing some basic maths which do not require any methods.in these cases structs will save you resources since they wouldn't need any memory addresss to access the data (not referenced).
User is offlineProfile CardPM
+Quote Post

baavgai

RE: Structs Vs Classes

28 Oct, 2007 - 07:53 AM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 4,347



Thanked: 410 times
Dream Kudos: 550
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
QUOTE(Footsie @ 26 Oct, 2007 - 01:38 AM) *
Isn't there a way to force someone to initialize all fields in a class?


Yes, a class constructor can force init parameters. The initialization here refers to nulls:
CODE
int i; // primative
String s; // class
DateTime d; // struct

Here, s is null, both i and d must have some value that's within their scope. I believe i starts with 0 and d might be DateTime.Now. (Sorry, not close to a dev box. )

That you can't have true nulls in non class types is such a bother that a NullObject design pattern is common. Such a pattern is even implicitly implemented in .NET 2.0 with constructs like DateTime ?d, which wraps d in an object for ease of processing.

QUOTE(garima @ 26 Oct, 2007 - 05:46 AM) *
A structure would be collection of related data, while a class would be collection of data and code which handles that data.


Problem: in C# structs can have methods! Now how do you explain the difference to a student? Structs have grown to blur the line, which is why they're such a pain to explain.

So, the reason to use these things is the stack? They look like objects to the user, until they are parameters, at which point copies are made and values never returned. Sure, you can use ref and break your OO even more. A careless list2=list1 will behave quite different for a struct than an object. And not having null is messy.

Using the stack has to be a really big bonus to recommend a struct. But in itself it's a negative, because now I have to think about stack and heap. Even better, it's far far easier to overflow a stack. Of course, even though objects live on the heap, their privative parts will ultimately find their way to the stack for processing. Some of the use the stack argument is lost there.

In an Object Oriented environment, primitives are a required evil. Structs, on the other hand, are an unsavory, ultimately unnecessary, blemish. There are few list processing examples you might come up with which recommend a struct, but I don't believe such C hold overs can really justify the thing's existence.

User is offlineProfile CardPM
+Quote Post

gogole

RE: Structs Vs Classes

28 Oct, 2007 - 08:10 AM
Post #10

D.I.C Head
Group Icon

Joined: 17 Jul, 2007
Posts: 131



Thanked: 1 times
Dream Kudos: 25
My Contributions
i guess you will not change your mind about structs in c#.understood,everybody has his style.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/20/09 09:54PM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month