2

如果我在 vm.TempRowKey 为空时执行以下分配,那么 newRowKey 的值是否会为空?

var newRowKey = vm.TempRowKey.DotFormatToRowKey();

如果 dotFormatRowKey 不具有 x 是数字的 xx 格式,还有一种方法可以使以下内容引发异常吗?

public static string DotFormatToRowKey(this string dotFormatRowKey) {
    var splits = dotFormatRowKey.Split('.')
                 .Select(x => String.Format("{0:d2}", Int32.Parse(x)))
                 .ToList();
    return String.Join(String.Empty, splits.ToArray());
}
4

6 回答 6

3

当 vm.TempRowKey 为空时

然后TempRowKey.DotFormatToRowKey();会抛出一个空引用异常。

如果 dotFormatRowKey 不具有格式 xx 其中 x 是数字,则抛出异常?

public static string DotFormatToRowKey(this string dotFormatRowKey) 
{
    if (dotFormatRowKey == null)
        throw new ArgumentNullException("dotFormatRowKey");    

    // maybe @"^\d\d?\.\d\d?$" is a beter regex. 
    // accept only 1|2 digits and nothing before|after
    if (! Regex.IsMatch(dotFormatRowKey, @"\d+\.\d+"))  
       throw new ArgumentException("Expected ##.##, was " + dotFormatRowKey);

    var splits = dotFormatRowKey.Split('.')
                 .Select(x => String.Format("{0:d2}", Int32.Parse(x)))
                 .ToList();  // ToList() is never needed

    // ToArray() not needed in Fx >= 4.0
    return String.Join(String.Empty, splits.ToArray()); 
}

小细节:您同时使用ToList()ToArray()splits这是双重工作,在 .NET 4 中您也不需要。

于 2012-09-22T13:24:05.060 回答
3

不,结果不会为空。您可以使用 null 引用调用扩展方法,但扩展方法不是为处理 null 值而编写的,因此当您尝试Split在 null 引用上使用该方法时,您会收到一个错误。

要检查格式“xx”,您可以检查 的结果的长度Split,然后用于TryParse检查这些值是否可以解析:

public static string DotFormatToRowKey(this string dotFormatRowKey) {
  var splits = dotFormatRowKey.Split('.');
  if (splits.Length != 2) {
    throw new FormatException("The string should contain one period.");
  }
  var s = splits.Select(x => {
    int y;
    if (!Int32.TryParse(x, out y)){
      throw new FormatException("A part of the string was not numerical");
    }
    if (y < 0 || y > 99) {
      throw new FormatExcetpion("A number was outside the 0..99 range.");
    }
    return y.ToString("d2");
  }).ToArray();
  return String.Concat(s);
}
于 2012-09-22T13:29:20.537 回答
0

对于您的第一个问题.. 尝试看看会发生什么。

第二个问题,您可以使用TryParse, 如果简单地失败了.. 抛出异常。

于 2012-09-22T13:22:57.283 回答
0

你必须检查它是否为空,否则你会得到一个异常。

if (null == dotFormatRowKey)
    return null;

您可以使用 Regex 验证模式。

Regex.IsMatch(input, "^\d+\.\d+$");

于 2012-09-22T13:24:43.827 回答
0

如果我在 vm.TempRowKey 为空时执行以下分配,那么 newRowKey 的值是否会为空?

这应该会导致 NullReferenceException,因为 dotFormatRowKey 将为 null,然后您将对 null 值调用 Split()(它不是扩展方法)。

如果 dotFormatRowKey 不具有 x 是数字的 xx 格式,还有一种方法可以使以下内容引发异常吗?

目前,使用 int.Parse() 强制您只有整数值和“.”。它不强制所有整数值都相同,也不强制只有 1 个“。” (例如,这不会在 1.2.3 上抛出)。如果您想添加额外的错误检查,这很容易做到:

// test that all int values were the same (not sure if you want this but you said x.x)
if (splits.Distinct().Count() > 1) { throw new ExceptionOfSomeSort("error"); }

// test that you have exactly two values
if (splits.Count != 2) { throw new ExceptionOfSomeSort("error"); }

另一种选择是使用正则表达式预先验证整个字符串,例如:

@"\d+\.\d+"
于 2012-09-22T13:28:51.443 回答
0
public static string DotFormatToRowKey(this string dotFormatRowKey)
        {
            Regex validationRegex = new Regex(@"\d.\d");
            if (!validationRegex.Match(dotFormatRowKey).Length.Equals(dotFormatRowKey.Length)) throw new FormatException("Input string does not support format x.x where x is number");

            var splits = dotFormatRowKey.Split('.')
                .Select(x => String.Format("{0:d2}", Int32.Parse(x)))
                .ToList();
            return String.Join(String.Empty, splits.ToArray());
        }
于 2012-09-22T13:33:09.513 回答