PLEASE NOTE: I am aware that a similar question has already been answered . It took me a while to find that question/answer because it assumes I recognized that it specifically involves extension methods, which I didn't at first. I was under the incorrect assumption that the static using
feature could only be used with extension methods, so because of its perceived superfluity I didn't include that when searching.
The purpose of this post is to help people who don't recognize that.
I have some code with the using static directive
that introduced in C# 6:
using static MyNs.MyExtensionMethods;
I am trying to use an extension method defined in MyNs.MyExtensionMethods
, and this works (as it always has):
foo.MyExtensionMethod();
But this does not:
MyExtensionMethod(foo);
I'm getting a compiler error "The name 'MyExtensionMethod' does not exist in the current context". What is going on?
As indicated by this answer , you cannot use the using static
directive to allow unqualified access to extension methods , only to regular static methods (ie without the this
keyword on the first argument). The reason is clearly stated in that answer, so I won't duplicate it here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.