如果将null传递给compare函数,则代码会抛出以下异常。
我如何处理null?
System.NullReferenceException:未将对象引用设置为对象的实例;
public static bool Compare(string s1, string s2)
{
var array1 = s1.ToUpper().ToCharArray();
var array2 = s2.ToUpper().ToCharArray();
int count1 = array1.Count();
int count2 = array2.Count();
int verify1 = 0;
int verify2 = 0;
int sum1 = 0;
int sum2 = 0;
for (int i = 0; i < count1; i++)
{
verify1 = (int)array1[i];
sum1 += (int)array1[i];
if (verify1 < 65 || verify1 > 90)
{
sum1 = 0;
count1 = i;
}
}
for (int j = 0; j < count2; j++)
{
verify2 = (int)array2[j];
sum2 += (int)array2[j];
if (verify2 < 65 || verify2 > 90)
{
sum2 = 0;
count2 = j;
}
}
if (sum1 == sum2)
{
return true;
}
else
{
return false;
}
}