开发者

CA1026 -- Can this message be suppressed if an alternate, CLS Compliant method is supplied in place?

According to CA1026: Default parameters should not be 开发者_StackOverflowused I'm not supposed to use default parameters.

MSDN says not to suppress the message:

Do not suppress a warning from this rule.

However, I wonder... if I include a CLSCompliant variation of the method, can I suppress this message? Or is having 2 variations of the same method, one with all default parameters, one empty, going to cause issues?

public class Foo
{
    public Foo()
        : this(0)
    {
    }

    [CLSCompliant(false)]
    [SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
    public Foo(Int32 id = 0)
    {
        //...
    }
}

Would that be an ok situation to ignore MSDN's suggestion? I'm clearly marking the method (constructor) as CLSCompliant(false) and providing a method (constructor) that does not take any default values.


A side note: it seems that this is in place because:

The compiler ignores the values of default parameters for Managed Extension for C++ when it accesses managed code.

... so, I wonder if I compile this way will that cause issues since I technically now have 2 constructors that can take 0 parameters?


Actually, you don't have 2 constructors without arguments. The default value is consumed only at compile time, and a compiler that supports default parameter values will usually also have a rule for resolving which method to target.

That said, why are you bothering to specify a default value given that you have an override without the parameter? The main reason folks like default parameter values is that they don't want to have to bother writing the overloads.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜