I write this code to subtract binary values , I need instead of for loop using, I need to use LINQ for performance issue , I tried but Ifailed, the code is
static void Main(string[] args)
{
byte[] data = { 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0 };
if (data[0] == 1)
{
byte[] One = new byte[data.Length];
One[One.Length - 1] = 1;
byte borrow = 0;
for (int i= data.Length -1 ; i > -1 ; i--)
{
if(borrow == 1 ) data[i] = 0;
if (data[i] == 0 && One[i] == 0)
{
borrow = 0; data[i] = 0;
}
else if (data[i] == 0 && One[i] == 1)
{
borrow = 1 ; data[i] = 1;
}
else if (data[i] == 1 && One[i] ==0)
{
borrow = 0; data[i] = 1;
}
else if (data[i] == 1 && One[i] == 1)
{
borrow = 0; data[i] = 0;
}
if (data[i] == 1) data[i] = 0; else data[i] = 1;
}
}
}
}
}
Linq doesn’t add any beneficiary to your sample code, It just make it little slower, because of some internally function calls, it just implements yield pattern in some cases, but your code doesn’t need this, Also with linq it’s more complicated, but your current code is easy to read. So I suggest do not use linq, except it make your life better. not doing everything with linq.