2

I read several threads on here about structs (one about mutable structs) and I keep reading about how a struct should have no identity.

What exactly is a lack of identity in a struct? I am guessing it would be like a number, e.g. 5, not having a context (5 what?). Whereas client would be someone expecting a service, and thus there is an identity. Am I thinking correctly?

I know the technical differences and how structs are thread safe (as long as they can't be mutated, but I can still write methods to mutate state), they have new copies everytime they are passed into a method, etc...

GurdeepS
  • 65,107
  • 109
  • 251
  • 387

3 Answers3

9

It means that the only data you have about a struct is the values is stores - there's no difference between one "5" and another "5". Compare this with a class:

Person person1 = new Person("Jon");
Person person2 = new Person("Jon");
Person person3 = person2;

Now person1 and person2 have references to distinct objects, although they both contain the same data. person2 and person3 both refer to the same objects. With structs this distinction doesn't exist.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

Struct is a value type, so it just represents a value. An Id would only be useful to create a reference to it, in which case it is better off being a class.

Mark Dickinson
  • 6,573
  • 4
  • 29
  • 41
0

I guess you're more or less influenced by Evans' book, where he distinguishes Entities and Value objects.

Anton Gogolev
  • 113,561
  • 39
  • 200
  • 288