0

So I've been trying to get my head around this for weeks. I understand that structs are value types, and that classes are reference types. Where I get confused is the differences in behavior between the two.

For example: If I have a bag of potatoes, and each potato is a different size, shape, and weight... The bag would be a 'class' / reference type The potatoes would be a 'struct' / value type

Can my potatoes be a class, or do they have to be a struct as the values contained within are different?

Basically, the answer to that question will clear everything up for me.

Thank you!

10
  • 12
    I highly doubt an answer to the question "Can my potatoes be a class?" clears up anything. Commented Jun 5, 2013 at 12:34
  • 2
    Keep in mind the difference between a language-based value/reference type and a logical value/reference type. A class may logically be a value type and internally maintains reference concepts like equality/immutability/etc. Commented Jun 5, 2013 at 12:35
  • Consider the kinds of things that are structs: int, float, decimal, Color; and the kinds of things that are classes: Form, Page, StackPanel, Socket. Commented Jun 5, 2013 at 12:36
  • This post explains very well when to use struct: stackoverflow.com/questions/521298/when-to-use-struct-in-c Commented Jun 5, 2013 at 12:36
  • The main difference is in "copy semantics" : What exactly happens with a = b; depends on rev or value type. Commented Jun 5, 2013 at 12:38

3 Answers 3

5

If all that you're tracking about potatoes is their size, weight and shape, then answer the question - "if two potatoes have the same size, weight and shape, should they be considered to be the same?". And the answer to that question depends on your problem domain.

If they should be "the same", then you're comparing them by value and (generally) could/should be value types. If they should not be treated as "the same", then they should be reference types.

Sign up to request clarification or add additional context in comments.

4 Comments

I agree, although I think that the concept of identity is orthogonal to whether something is a reference or value type. I cite the example of the string class which is a reference type, but it behaves like a value type. But you did say "generally", so I agree. :)
Yeah, I wouldn't rely on discussing comparing since you can quite happily compare reference types by value...
Having "Identity" conflicts with Value types. Not having Identity (string) does not conflict with Reference type.
@HenkHolterman Yes, that makes the question easier to answer. If something has "identity" then it cannot be a value type.
1

If you want to take a Potato out of BagOfPotatoes and pass it around between other classes, if these classes were to modify the potato then it should be a reference type.

For example, BagOfPotatoes is passed to Chef.SortPotatoes(List<Potatoes> potatoes) and then goes on to Chef.SkinPotatoes(Potato potato) where the chef may skin the potato, setting Potato.IsSkinned.

  • If it were a reference type - Then any values changed would also be changed in BagOfPotatoes. i.e. Potato.IsSkinned = true

  • If it were a value type - Then BagOfPotatoes would still contain a bag with the original Potatos, i.e. Potato.IsSkinned = false

It deppends on whether or not you need to refer to a potato as an individual potato, where you chuck it about, and still talk about the same mashed up potato at the end of it (by reference). Or whether you only want to talk about the type of potato that it is (by value).

1 Comment

+1 for making the best of an awful analogy.
0

Anything that is a struct could be a class instead. The two behave subtly differently, but your "potatoes" could certainly be a class instead of a struct.

1 Comment

I disagree, potatoes are inherently value types. I think you're confusing with tomatoes, which are definitely reference types.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.