我创建了 c# 的函数来比较我项目中的价格。

public static decimal? SpecialPrice(decimal w, decimal p)
{
    if (w < p)
    {
        return p;
    }
    else if (w > p && p > 0)
    {
        //return (w , p);
    }
    else
    {
        return w;
    }
}

当 w > p 和 p >0 时,我想返回两个变量( w 和 p),但我不知道如何返回。有人知道吗?

最佳答案

您可以使用 Tuple :

public static Tuple<decimal?, decimal?> SpecialPrice(decimal w, decimal p)
{
   if (w < p)
   {
      return new Tuple<decimal?, decimal?>(p, null);
   }
   else if(w > p && p>0)
   {
      return new Tuple<decimal?, decimal?>(p, w);
   }
   else
   {
      return new Tuple<decimal?, decimal?>(w, null);
   }
}

关于c# - 如何在c#方法中返回2个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8570452/

10-12 12:44
查看更多