扩展方法的建议前提条件 [英] Suggested precondition on extension method
本文介绍了扩展方法的建议前提条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我认为对扩展方法建议Contract.Requires(i!= null)毫无意义。
例如:
公共接口IInterfaceable {}
> public static class Extend
{{ public static void DoNothing(这个IInterfaceable i)
{
      Contract.Requires(i!= null); //这样做有什么意义?
}
解决方案
没有什么能阻止方法像普通的静态方法那样被调用,在这种情况下,可以为"..."传递空值。这" 。参数 例如: -
Extend.DoNothing(空)
I think there is no point in suggesting Contract.Requires(i != null) on extension method.
Example:
public interface IInterfaceable { }
public static class Extend
{
public static void DoNothing(this IInterfaceable i)
{
Contract.Requires(i != null); //what is the point in doing this?
}
}
解决方案
There's nothing preventing the method from being called like a normal static method, in which case a null value could be passed for the "this" parameter. e.g.:
Extend.DoNothing(null)
这篇关于扩展方法的建议前提条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文