简体   繁体   中英

How do I call a C# extension method from F#, without open?

I have an extension method in C#:

Foo(this Bar bar, int x)
{
    // Do stuff
}

How do I call this in F#?

bar.Foo(100); // Doesn't work
bar.Namespace.Foo(100); // Doesn't work

The answer to How do I use the extension method Sum on a .NET list in F#? requires opening the module. Can I access the extension method without open ?

This issue is the same in F# and C#. To use an extension method, you either open the namespace (F#) or have a using statement for the namespace (C#), or you fully-qualify the method if for some reason you can't or don't want to open the namespace.

In both languages, with the System.Linq namespace open:

myArray.Sum()

In both languages, without the System.Linq namespace open:

System.Linq.Enumerable.Sum(myArray)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM