-
October 8th, 2014, 03:06 PM
#1
Should a type for working with fractions be a class or a struct?
Folks,
I’m making a C# type for working with fractions, and trying to follow best practices. My question is: should a type like this be a class (reference type) or a struct (value type)?
Here’s what I’m thinking.
- Fraction is a kind of a number, which makes me think that it should be a value type, and therefore a struct.
- System.Numerics.Complex has some similarities with a fraction type. Both represent a kind of a number. Both are small with only 2 numeric fields. System.Numerics.Complex is a stuct.
At the same time, I’ve found a couple of existing articles on CodeProject, which describe Fraction types. Both of them are classes.
The authors don’t explain why they chose to declare classes rather than structs. Something like that makes me think that there may be something that I don’t understand that was fairly obvious to them.
Any suggestion, insight or reference is really appreciated!
Cheers,
- Nick
P.S. For the most part, I work mostly with C and C++, where I have fine control over things like const, pointers, references. Of course, that comes at a risk of having nasty pointer-related bugs.
Last edited by kender_a; October 8th, 2014 at 03:22 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|