I have the following code in my application:
SqlParameter[] sqlCmdParameters=new SqlParameter[0];
Later, I am passing the array to a method as following:
void CallDB( SqlParameter[] sqlCmdParameters)
{
if (sqlCmdParameters == null && sqlCmdParameters.Length>=0 )
{
return;
}
Console.Writeline(sqlCmdParameters[0].value);
}
The above code encounters an "Object reference not found exception" as the array is empty. I could perform a element wise null checking in the loop but I think that would not be a good approach. What is the best practice to check for empty array in C#? Also, why an empty array length is 1 when there is no element at all?