I am fairly new to C# coming from Java, and I'm wondering if there's a simple way to avoid code repetition involving primitive types like this:
private Boolean AtLeastOneBufferItemIsNonZero(int[] Buffer)
{
Boolean result = false;
foreach (int Item in Buffer)
{
result = !(Item == (int)0);
if (result) break;
}
return result;
}
private Boolean AtLeastOneBufferItemIsNonZero(float[] Buffer)
{
Boolean result = false;
foreach (float Item in Buffer)
{
result = !(Item == (float)0);
if (result) break;
}
return result;
}
I can't find a "Number" supertype so that I can compare "Item" in a generics implementation (I wouldn't mind the performance penalty of boxing, although I understand that in .NET there is no such thing?):
//SOMETHING LIKE THIS?
private Boolean AtLeastOneBufferItemIsNonZero<T>(T[] Buffer) where T : NUMBERTYPE
{
Boolean result = false;
foreach (T Item in Buffer)
{
result = !(Item.Equals(0)); //Nope....
if (result) break;
}
return result;
}
Is the only way to create my own Number implementation and having a compare() method? That sounds like overkill doesn't it?