可选参数或重载 [英] Optional Parameters or Overloading

查看:84
本文介绍了可选参数或重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



这可能不是最常见的问题,但它让我烦恼。



让我们说,我有一个方法会超载几次,以便将所有逻辑放在一个地方,这样(愚蠢的例子,但它覆盖了我所说的):



Hello,

This may not be the most common question, but it's bugging me.

Lets say, I have a method that is overloaded a couple times to have all logic in one place like this(dumb example, but it kind covers what I'm saying):

public int sum(int x, int y) 
{
   return sum(x,y,0,0,0);
}

public int sum(int x, int y, int w) 
{
    return sum(x,y,w,0,o);
}

public int sum(int x, int y, int w, int z) 
{
    return sum(x,y,w,z,0);
}

public int sum(int x, int y, int w, int z, int a) 
{
    int result = x+y+w+z+a;
    return result;
}





以及使用可选字段制作单一方法的选项





and the option to make a single method with optional fields

public int (int x, int y, int w = 0, int z = 0, int a = 0)
{
    int result = x+y+w+z+a;
    return result;
}





所有解释,以下是问题。它们在性能和开销方面是否相同?拜托,任何人都可以告诉我它是否会在IL上结束相同而且我在说疯狂的东西:P?

计划是在一个规模很大的项目上实现这个,所以如果有人在两种用途(好的和坏的)都有一些经验,我将很高兴听到。



问候



All explained, here are the questions. Do they become the same in terms of performance and overhead? Please, can anyone tell me if it will end up the same on the IL and I'm talking crazy stuff :P ?
The plan is to implement this on a good size project, so if someone has some experience on both uses(good and bad), I would appreciate to hear from.

Regards

推荐答案

他们当然不会生成相同的IL - 重载版本将生成四种方法,而可选参数版本将只生成一个。



使用重载版本,更容易更改参数的默认值。使用可选参数,默认值为烘焙到调用者,因此您必须重新编译所有内容才能使新默认值生效。



如果你'不要小心,可选参数会引入版本问题,正如Phil Haack在2010年所描述的那样:

使用可选参数进行版本控制问题 [ ^ ]

使用可选参数进行更多版本控制的乐趣 [ ^ ]



接口和子类可以让生活变得有趣以及:

C#基础:可选参数 - 优点和缺陷 [ ^ ]

They certainly won't produce the same IL - the overloads version will generate four methods, and the optional parameters version will only generate one.

With the overloads version, it's easier to change the default value of a parameter. With optional parameters, the default value is "baked in" to the caller, so you would have to recompile everything for the new default to take effect.

If you're not careful, optional parameters can introduce versioning problems, as Phil Haack described in 2010:
Versioning Issues With Optional Arguments[^]
More Versioning Fun With Optional Arguments[^]

Interfaces and sub-classes can make life interesting as well:
C# Fundamentals: Optional Parameters - Pros and Pitfalls[^]
public interface ITag 
{
   void WriteTag(string tagName = "ITag");
} 

public class BaseTag : ITag 
{
   public virtual void WriteTag(string tagName = "BaseTag") 
   { 
      Console.WriteLine(tagName); 
   }
} 

public class SubTag : BaseTag 
{
   public override void WriteTag(string tagName = "SubTag") 
   { 
      Console.WriteLine(tagName); 
   }
} 

public static class Program 
{
   public static void Main() 
   {
      SubTag subTag = new SubTag();
      BaseTag subByBaseTag = subTag;
      ITag subByInterfaceTag = subTag; 

      subTag.WriteTag();             // Output: SubTag
      subByBaseTag.WriteTag();       // Output: BaseTag
      subByInterfaceTag.WriteTag();  // Output: ITag
   }
}


在这种情况下,我会投票选择可选参数,因为你所做的似乎与我正交:没有显着的变化。



你也可以考虑使用Enumerable.Sum:
In this case I would vote for optional parameters because what you are doing seems "orthogonal" to me: there's no significant variation.

You could also consider using Enumerable.Sum:
// requires Linq and .NET 3.5
public int SumAll(params int[] values)
{
    return values.Sum();
}

'Enumerable的Sum重载允许您使用各种类型和可选的Func进行转换,或者其他:[ ^ ]。



您甚至可以使用Array.ForEach(.NET 3.0)。



我怀疑所有这些实现都表现同样出色鉴于你正在做的事情的简单性,以及.NET编译器优化的当前复杂性。



从长远来看,我认为让你的代码自我-documenting ...在代码结构中表达你的意图,因此很明显,有时候,对其他人来说......在你用来实现这个方法的语法中可能同样重要。

The 'Sum overloads of Enumerable allow you to use a wide variety of Types and optional Func for transformation, or whatever: [^].

You could even use Array.ForEach (.NET 3.0).

I suspect that all of these implementations would perform equally well given the simplicity of what you are doing, and the current sophistication of the .NET compiler's optimizations.

And, in the long run, I think that making your code self-documenting ... expressing your intent in the structure of the code so it is is clear, over times, to others ... can be just as important a factor in the syntax you use to implement this method.


历史上,没有引入选项参数。在C#的更高版本中,已添加此功能。也就是说,可选参数对开发人员有额外的价值。我认为这个值很明显:你的方法较少;整个过程更容易维护;你的代码较少;代码总体上更具可读性。



你是那个必须决定的人。比方说,如果有可能将代码降级到旧版本的C#,使用重载更安全,不需要将代码修改回重载。在所有其他情况下,我更喜欢可选参数。但你决定。







我在评论中详细讨论了性能方面的考虑因素。这个答案如下。



-SA
Historically, option parameters weren't introduced. In later versions of C#, this feature have been added. That said, optional parameters have additional value for developers. I think this value is quite obvious: you have less methods; the whole thing is somewhat easier for maintenance; you have less code; the code is more readable overall.

You are the one who has to decide on that. Say, if there is a chance of down-grading the code to older versions of C#, using "overloading" is safer, won't require modification of code back to "overloading". In all other cases, I would prefer optional parameters. But you decide.



I discussed performance considerations in detail, in the comments to this answers, below.

—SA


这篇关于可选参数或重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆