Coming from a non-OO background in Matlab, I'm looking to perform some of the operations I am used to in a best-practice manner for C#.
I have a class, which imports data from a source, and formats it into, amongst other things, an array of ints. I am going to want to perform some operations on this array. To keep it simple, lets say I want to take the average of all the values. The way I see it, I have three options:
double arrayAverage=MyClass.ArrayAve;
I could develop a static class, giving me access to the average by double arrayAverage= zotty.arrayMath.average(MyArray);
There is apparently some way in which I can inherit or interface so that I create a class specifically for performing such operations, which Id then be able to access by instantiating this new class, and passing it the array:
OperatorClass oc = new OperatorClass();
oc.data=MyClass.MyArray;
double arrayAverage = oc.ArrayAverage();
Extending the class seems risky, since it's also responsible for importing and formatting the data. I could become quite large if I add all the data processing to it too, and it wouldn't be straightforward for other people to add-to & work on alongside me.
Future code will require more complex operations on the array, and I've no idea what they will be at the moment. I will have use for some of the basic operations in future programs, which would be useful as part of the static class - the complex ones however would be redundant.
The separate class for performing the data processing allows the import/formatting class to remain isolated. It can provide both general and specific functions, but at the cost of instantiating another variable in the code, which seems to me to be a bit untidy.
I'd appreciate some input if you have thoughts on the matter!
You can simply expose the array as IEnumerable<int> and then use any LINQ method, such as Average:
MyClass myObject = ...
double a = myObject.Data.Average();
where
class MyClass
{
private int[] data;
public IEnumerable<int> Data
{
get
{
return this.data;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With