无法使用ref和out的第一(QUOT;本")参数的扩展方法? [英] Impossible to use ref and out for first ("this") parameter in Extension methods?
本文介绍了无法使用ref和out的第一(QUOT;本")参数的扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么禁止呼叫扩展方法
与 REF
修改?
Why is it forbidden to call Extension Method
with ref
modifier?
这一个是可能的:
public static void Change(ref TestClass testClass, TestClass testClass2)
{
testClass = testClass2;
}
而这其中并不:
And this one not:
public static void ChangeWithExtensionMethod(this ref TestClass testClass, TestClass testClass2)
{
testClass = testClass2;
}
但是,为什么?
But why?
推荐答案
您必须指定 REF
和退出
明确。你会怎么做这与扩展方法的?此外,你会真正的需要的到?
You have to specify ref
and out
explicitly. How would you do this with an extension method? Moreover, would you really want to?
TestClass x = new TestClass();
(ref x).ChangeWithExtensionMethod(otherTestClass);
// And now x has changed?
或者你想不必指定 REF
部分,只是在扩展方法的第一个参数?
Or would you want to not have to specify the ref
part, just for the first parameter in extension methods?
这只是听起来怪我,说实话,和一个配方不可读(或至少难以于─predict)code。
It just sounds weird to me, to be honest, and a recipe for unreadable (or at least hard-to-predict) code.
这篇关于无法使用ref和out的第一(QUOT;本")参数的扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文