在成员初始不能用'这个'? [英] Cannot use 'this' in member initializer?
本文介绍了在成员初始不能用'这个'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是合法的吗?它含有一个隐藏的错误或缺陷? Visual Studio中并没有给出任何错误或警告,但ReSharper的作用:
///<总结>
///一成不变的元组两
///< /总结>
公共类对< TValue1,TValue2> :辛格尔顿< TValue1>
{
公共TValue2值2 {搞定;私人集; }
公众对(TValue1值1,TValue2值2,Func键<对< TValue1,TValue2>中字符串> toStringFunc)
:这(值1,值2,()=> toStringFunc(本)){} //红灯
} 2 - ; :辛格尔顿< TValue1>
解决方案
我敢肯定,我听说这是一个编译器错误,固定在下一版本中。我只是发射了我的4.0虚拟机,用一个简单的测试用例:
类Foo {
公共美孚():这个(委托{this.Bar();}){}
公共美孚(动作富){}
公共无效栏(){}
}
工作在VS2008,但VS2010:
错误1关键字'这个'是不是在目前的情况下
块引用>
酒店Is this legal? Does it contain a hidden bug or flaw? Visual studio does not give any errors or warnings but ReSharper does:
/// <summary> /// immutable tuple for two /// </summary> public class Pair<TValue1, TValue2> : Singleton<TValue1> { public TValue2 Value2 { get; private set; } public Pair(TValue1 value1, TValue2 value2, Func<Pair<TValue1, TValue2>, String> toStringFunc) : this(value1, value2, () => toStringFunc(this)) { } //Red light }2> : Singleton<TValue1>
解决方案I'm pretty sure I've heard that this is a compiler bug, fixed in the next release. I'm just firing up my 4.0 VM, with a simpler test-case:
class Foo { public Foo() : this(delegate { this.Bar(); }) { } public Foo(Action foo) {} public void Bar() {} }
works in VS2008, but in VS2010:
Error 1 Keyword 'this' is not available in the current context
这篇关于在成员初始不能用'这个'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文