Why can you not invoke extension methods directly?

后端 未结 4 949
离开以前
离开以前 2021-01-11 16:13

Can someone explain to me why in the following the 3rd invocation of DoSomething is invalid? ( Error message is \"The name \'DoSomething\' does not exist in the current cont

4条回答
  •  粉色の甜心
    2021-01-11 16:57

    Because DoSomething takes a parameter.

    DoSomething(a) would be legal.

    Edit

    I read the question a bit wrong here.

    Since your calling it a a normal static method, and not a extension method, you need to prefic with the class name.

    So A_Ext.DoSomething(a); will work.

    If you call it like a normal static method, all the same rules apply.

    Your second variant works because B inhetits A, and therefore you still end up calling it as an extension method, but the third does not.

    sorry about the first version above that does not work. I'll leave it to keep the comment relevant.

提交回复
热议问题