0

I have a class which is quite Maths intensive and performs a number of formulas/equations on an object (object not being a class in my code but a real world object).

Anyway, I was wondering, under my class should I move all the properties and fields into a struct? Would this be a bad design?

A struct is supposed to be useful for something like this from what I have learnt. It will also mean I can return the struct from a method and make the code a bit more atomic (struct has data, class has logic). If I keep the current design and return a class with written variables, what difference will this make to returning a struct? I'm aware of the differences between a struct and class but I am not sure if they will have much effect.

Thanks

GurdeepS
  • 65,107
  • 109
  • 251
  • 387

3 Answers3

4

Your definition ("struct has data, class has logic") of struct vs class is incorrect for .NET; structs can have logic perfectly well. Structs define a type with value-type semantics. Classes define a type with reference-type semantics. That is the only (albeit major) difference.

I wouldn't, no. Structs in .NET should generally be immutable; this would force you to update all the values at once. With a mutable class, you can update individual properties / fields as you like. In a CPU-intensive use-case designed around mutating objects, this could make a big difference. Of course, if you don't mutate state, it doesn't matter as much, but I might still be inclined to stick with a class:

When you pass a class instance around, all you pass is the reference; with a struct, you need to clone and pass around as much data as is in the struct. This can make intensive work more intensive when using struct (unless you use ref a lot).

Re "and return a class with written variables" - do you mean public fields? This is not recommended. Properties are far preferable, for countless oft-repeated reasons.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

The "structs have data and classes have logic" idea is a misunderstanding of the concept. It is just not true in C#. From the C# Language Specification 3.0:

Like classes, structs are data structures that can contain data members and function members, but unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly stores the data of the struct, whereas a variable of a class type stores a reference to a dynamically allocated object.

Jon Skeet wrote an excellent answer about classes and structs here on Stack Overflow. I recommend reading it. It is just pointless to duplicate it here. Also the other answers in that question are of very good quality as well.

To answer your question: It is kind of pointless to group the all of the data in a class into an other class just because you can. Believe me, even if that would speed up things (which is something I doubt,) this is not going to be a bottleneck. If some of the data can be grouped into subgroups, you probably need to refactor your class anyway into smaller classes. (Not just the data though but probably the function members too)

Community
  • 1
  • 1
Tamas Czinege
  • 118,853
  • 40
  • 150
  • 176
1

Thanks guys.

I will stick with returning a class for the substantial reasons above.

re returning a class with written variables - I always use properties to read/write to. Most immediate reason for this is that I can provide logic to the property when required (e.g. if value is less than 18, throw an exception of some sort). And of course, more control, like making it only visible to the assembly etc.

GurdeepS
  • 65,107
  • 109
  • 251
  • 387
  • Good choice ;-p Re the fields: if I misunderstood you and you just mean manually implemented properties rather than auto-implemented properties (C# 3.0), then that is fine - no problem there, as long as you generally use the properties ;-p – Marc Gravell Jan 13 '09 at 23:24
  • Not at all. I tend to not make myself clear - and I am a coder, lol. A variable would generally mean I'm using fields, which is confusing English from me. – GurdeepS Jan 14 '09 at 18:15