CA1026 -- 如果提供了替代的、符合 CLS 的方法,是否可以抑制此消息?
根据 CA1026:不应使用默认参数 我不是应该使用默认参数。
MSDN 说不要抑制该消息:
不要抑制此警告 规则。
但是,我想知道...如果我包含该方法的 CLSCompliant 变体,我可以抑制此消息吗?或者同一方法的两种变体(一种具有所有默认参数,一种为空)会导致问题吗?
public class Foo
{
public Foo()
: this(0)
{
}
[CLSCompliant(false)]
[SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
public Foo(Int32 id = 0)
{
//...
}
}
在这种情况下可以忽略 MSDN 的建议吗?我明确地将方法(构造函数)标记为 CLSCompliant(false)
并提供一个不采用任何默认值的方法(构造函数)。
附注:这似乎是因为:
编译器忽略以下值 托管的默认参数 C++ 访问时的扩展 托管代码。
...所以,我想知道我这样编译是否会导致问题,因为从技术上讲我现在有 2 个可以接受 0 个参数的构造函数?
According to CA1026: Default parameters should not be used 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,你没有两个没有参数的构造函数。默认值仅在编译时使用,支持默认参数值的编译器通常还会有一个规则来解析要定位的方法。
也就是说,既然您有一个没有参数的覆盖,为什么还要费心指定默认值呢?人们喜欢默认参数值的主要原因是他们不想费心编写重载。
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.