【问题标题】:Is there any better way to compare these values in C#?有没有更好的方法来比较 C# 中的这些值?
【发布时间】:2013-06-26 21:24:41
【问题描述】:

我有这样的比较值的方法:

protected bool CompareValues(string a="", int b=0, string c="", int d=0, string e="", int f=0)
{
int counter = 0;

if(int.Parse(a) > b)
{
  counter++;
}
if(int.Parse(c) > d)
{
  counter++;
}

if(counter > 1)
{
 counter = 1;
}

 if(int.Parse(e) > f)
{
  counter++;
}

if(counter > 1)
{
  return true;
}
else
{
 return false;
}

}

它对我来说很好,但如果可能的话,我不能不考虑一些改进。任何建议将不胜感激。

【问题讨论】:

  • 你想达到什么目的?
  • 你为什么要增加 counter 只是为了把它设置回一个?
  • 我认为这很明显。我正在比较值并根据结果返回真或假。这是我最近的项目的要求
  • 该死的,伙计。变量名不花钱。
  • 首先,为什么在这里提供默认值?其次,为什么要提供会让方法抛出异常的默认值?

标签: c# compare


【解决方案1】:

如果需要对表单进行 n 次比较

(int.Parse(a1) > b1 || int.Parse(a2) > b2 || ... || int.Parse(aK) > bK) && int.Parse(aN) > bN

您可以创建一个只接受一组值对进行比较的方法

protected bool CompareValues(params Tuple<string, int>[] comparisons)
{
    if(ReferenceEquals(comparisons, null))
    {
        throw new ArgumentNullException("comparisons");
    }

    if(comparisons.Length < 1)
    {
        throw new ArgumentException("At least one pair to compare must be specified");
    }

    var atLeastOneComparisonSucceeded = comparisons.Length == 1;

    for(var i = 0; !atLeastOneComparisonSucceeded && i < comparisons.Length - 1; ++i)
    {
        atLeastOneComparisonSucceeded = int.Parse(comparisons[i].Item1) > comparisons[i].Item2;
    }

    var lastIndex = comparisons.Length - 1;
    return atLeastOneComparisonSucceeded && int.Parse(comparisons[lastIndex].Item1) > comparisons[lastIndex].Item2;
}

用法:

var result = CompareValues(new Tuple<string, int>("5", 2), 
                           new Tuple<string, int>("3", 1), 
                           new Tuple<string, int>("1", 2));

如果您只需要 3 对值(如您的原始帖子中所示),您可以为提供适当默认值的方法提供重载,如下所示

    protected static bool CompareValues(string a, int b)
    {
        return CompareValues(a, b, "1", 0);
    }

    protected static bool CompareValues(string a, int b, string c, int d)
    {
        return CompareValues(a, b, c, d, "1", 0);
    }

    protected static bool CompareValues(string a, int b, string c, int d, string e, int f)
    {
        return ((int.Parse(a) > b || int.Parse(c) > d) && int.Parse(e) > f);
    }

当然,必须选择从重载传递下来的参数,以便语义合适。

【讨论】:

  • 这是我一直在寻找的最佳答案。我不想以我在问题中所写的方式指定所有参数,因为如果参数数量随着时间的推移而增加,它看起来会很丑
【解决方案2】:

我不确定你为什么将计数器重置为 1,但这是我理解的

if((int.Parse(a) > b || int.Parse(c) > d) && int.Parse(e) > f)
{
   return true;
}
else
{
   return false;
}

【讨论】:

  • 没错。我很感兴趣有没有其他方法可以以其他形式传递参数?
  • 我不确定您所说的其他形式是什么意思。您是指其他类型,例如 object 或 var?
  • 其他形式,我的意思是更短的方式
【解决方案3】:

看起来像你想要的:

return (int.Parse(a) > b || int.Parse(c) > d) && int.Parse(e) > f;

【讨论】:

    【解决方案4】:

    我不确定,但这是你想要做的吗?

    return ( ( int.Parse(a) > b || int.Parse(c) > d ) && int.Parse(e) > f);
    

    【讨论】:

    • 是的。我对此没有问题,我只是感兴趣是否有比我的示例更好的参数传递方式。
    • 更好是什么意思?
    • 我的意思是如果我必须比较超过 6 个值会发生什么。
    • 你可以试试params,它允许你指定一个方法参数,该参数接受一个参数,其中参数的数量是可变的。 Read more
    • 这就是我一直在寻找的答案。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多