C#中的可选参数或方法重载? [英] C# Optional Parameters or Method Overload?

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

问题描述

由于C#中添加了可选的参数是它认为更好的做法是使用可选参数或方法重载还是有,你可能需要使用一个比其他一个特殊情况。即功能瓦特/大量的参数,会更适合瓦特/可选参数?

Since C# added optional parameters is it considered a better practice to use optional parameters or method overloads or is there a particular case where you would want to use one over the other. i.e a function w/ lots of parameters would be better suited w/ optional parameters?

推荐答案

可选参数都不错,但应该是当它是有道理的使用。可选参数往往浑方法的意图 - 如果有另一种选择我会偏向另类。

Optional parameters are nice, but should be used when it makes sense. Optional parameters often muddy the intent of the method -- If there is another alternative I would lean towards the alternative.

的需要可选参数部分,并命名参数是因为COM允许可选的名称参数:

Part of the need for optional parameters and named parameters is because COM allowed optional and name parameters:

< STRONG> MSDN

一些API的,最主要的是COM接口
,如办公自动化的API,
被一个名为
,并考虑可选参数而编写的。高达
到现在就已经很痛苦了
调用在C#中这些API,具有显式地传递,最

有时多达30几个参数
,其中有合理的默认
值,是可以忽略

Some APIs, most notably COM interfaces such as the Office automation APIs, are written specifically with named and optional parameters in mind. Up until now it has been very painful to call into these APIs from C#, with sometimes as many as thirty arguments having to be explicitly passed, most of which have reasonable default values and could be omitted.

这forums.asp.net SomeNewKid简洁所说:

SomeNewKid from forums.asp.net puts succinctly:

http://forums.asp.net/t/386604的.aspx / 1

...重载方法一般是
优于可选参数。
为什么呢?为了让您的每一个方法
清晰的目的。也就是说,每个方法
应该做的一件事。只要
你介绍可选参数,你
被冲淡了
法的清洁,并引入分支
的逻辑,最好还是保持了
的方法。当你
开始使用继承的目的
的这种清晰就显得更为重要。如果
覆盖有一个或多个
。可选参数的方法,它们成为
更难进行工作。所以,我建议
,对于比快速和
类脏东西等,您使用
优先于可选参数超载。

...overloaded methods are generally preferable to optional parameters. Why? To keep each of your methods clear in purpose. That is, each method should do one thing well. As soon as you introduce optional parameters, you are diluting the cleanliness of that method, and introducing branching logic that is probably best kept out of a method. This clarity of purpose becomes even more important when you start using inheritance. If you override a method that has one or more optional parameters, they become harder to work with. So, I'd suggest that for anything other than quick and dirty classes, you use overloading in preference to optional parameters.

请记住,可选参数是一个语法糖:

Keep in mind that optional parameters are a syntactical sugar:

反射C#:

public class Class1
{
    // Methods
    public Class1()
    {
        this.Method1("3", "23");
    }

    public void Method1(string one, [Optional, DefaultParameterValue("23")] string two)
    {
    }
}

IL:

.class public auto ansi beforefieldinit Class1
    extends [mscorlib]System.Object
{
    .method public hidebysig specialname rtspecialname instance void .ctor() cil managed
    {
        .maxstack 8
        L_0000: ldarg.0 
        L_0001: call instance void [mscorlib]System.Object::.ctor()
        L_0006: nop 
        L_0007: nop 
        L_0008: ldarg.0 
        L_0009: ldstr "3"
        L_000e: ldstr "23"
        L_0013: call instance void WebApplication1.Class1::Method1(string, string)
        L_0018: nop 
        L_0019: nop 
        L_001a: ret 
    }

    .method public hidebysig instance void Method1(string one, [opt] string two) cil managed
    {
        .param [2] = string('23')
        .maxstack 8
        L_0000: nop 
        L_0001: ret 
    }

}

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

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