public static void outputDetail(DateTime previousTime, ref double[] array, StreamWriter streamWriter) //the parameter in here is not necessary, but want to maintain a similiarity in the TimeOfDay class
{
string outputString = previousTime.ToString("yyyy/MM/dd");
Boolean bypass = true;
for (int i = 1; i < array.Length - 1; i++)
{
outputString = outputString + "," + array[i].ToString();
if (array[i] != 0)
bypass = false;
}
if (bypass == false)
streamWriter.WriteLine(outputString);
for (int i = 0; i < array.Length; i++)
{
array[i] = 0;
}
}
public static void outputDetail(DateTime previousTime, ref int[] array, StreamWriter streamWriter) //the parameter in here is not necessary, but want to maintain a similiarity in the TimeOfDay class
{
string outputString = previousTime.ToString("yyyy/MM/dd");
Boolean bypass = true;
for (int i = 1; i < array.Length -1; i++)
{
if (array[i] != 0)
{
outputString = outputString + "," + array[i].ToString();
bypass = false;
}
else
{
outputString = outputString + ",";
}
}
if (bypass == false)
streamWriter.WriteLine(outputString);
for (int i = 0; i < array.Length; i++)
{
array[i] = 0;
}
}
they are exactly the same, only one takes a double array and one takes an int array, i see some example use Iconvertible but i cant get the syntax right. can someone post some workable snippet for the method pls?
can how do i call it?
EDIT: thanks very much for the answer, i have another somewhat more complicated case i need to refactor, and the suggestion in here dont work on that 2 methods. Please click this link for more detail
Change your code to this:
What’s changed?
First the method signature: it accepts a generic array of type
T(so it doesn’t matter if it’sint,double,boolorstrings).Now you have to fix the comparison for zero. Zero is the default value for both int and double so you can use default(T) to get the default value for actual type and
Object.Equals()for comparison (==and!=operators aren’t defined for generic types).Finally you just have to clear the array (again with zero) so you can simply use
Array.Clear()to do all the job (and it’s even little bit faster than a handmadefor).