In C# a struct is a bit like a class, but most of the time used for little items you might have. A struct is also a value type, while a class is a reference type. Now .NET is poorly equipped with math features. As I wanted to play around with complex numbers(yes I know I'm a wierd guy:icon_eek: ) I searched the web. Found lots of things, but one was missing this, the other was missing that, different naming conventions were used for the same thing, so I decided(just for fun:icon_eek: ) to write my own. Now the most implementations out there use a struct to implement a complex number.
So I did too.
But as I'm writing and testing my struct, I'm over 60 methods and properties for the moment and growing. (Among others I still have to implement the complex inverse trigonometric hyperbolic functions;) )

My question is: Should I not change my struct, which seems to become bigger and bigger to a class?
In other words, should I struct or should I Clash?

Recommended Answers

All 7 Replies

Its hard to say without having a look at it but you probably want to use a struct here since math related functions across the .NET platform are structs. Take a look at the DateTime struct. It has a number of methods (not nearly 60!) and implements a number of interfaces.

For math/numbers/etc in general you want them to be by value and not reference so this is another reason I would stay with a struct. You could also break out some methods in to a static class if you need 3+ of your 'large number structs' in order to perform a meaningful calculation because at that point it doesn't really make sense to have that method a member of the struct. I wouldn't want to look at something like someStruct.Calculate(otherStruct1, otherStruct2) .

Good Evening everybody, It's 7:10 PM.
Classes - When the size of data is unknown (it can be grow or shrink during runtime) then we must have to use class. Reference types uses managed heap.
Structs - Fixed length or fixed size data. Value type uses stack memory (size of stack memory is 1 MB approx).

@Scott
Yeah, forgot about the DataTime struct which also rather big.
OK I'll continue with it
@adatapost
That is what I am a bit affraid of : stack overflow, if I use large amounts of those structs.

What about creating a small complex number struct and dividing methods around it in different classes?

Not a bad idea! Thanks.
I don't know exactly what you mean by "dividing" I hope you don't mean "deriving", because a struct cannot be the base of a class.

Dividing means dividing the big (complex number) struct in to several other classes. Or I should say regrouping all your methods in to several classes.
I am sure you wont use all of the methods at once. You would be using group of method in one app and another group of method in another app.
Dividing them will help you to use less memory by loading object of small classes. I hope i made my point clear.

Thank you DangerDev for your explanation.
Also thanks to sknake and adatapost.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.