9.5 using the F # Library in C #

Source: Internet
Author: User

9.5 using the F # Library in C #

Like C #, F # is also a statically typed language, meaning that the compiler knows the type of each value and the signature of the class method and property. This is important for interoperability with C # because the compiler can generate code that looks like a normal. NET library.

Interoperability with other. NET languages

The interoperability between F # and C # or vb.net is very smooth, compared to languages that have a dynamic type of. NET implementation, such as Python, Ruby, and JavaScript. The compilers of these languages do not know the type of parameter required by the method, whether it is an int, or a customer, so it is difficult to use code written in these languages when using C # 3.0. In general, it is not even known whether an object contains a method with a particular name, so the C # code looks like this:

Obj. InvokeMethod ("SayHello", newobject[] {"Tomas"});

This example specifies the name of the method as a string and passes the parameter value to the method as an array. This is an important issue for many languages, so C # 4.0 introduces a dynamic type, which allows this to be written:

Obj. SayHello ("Tomas");

Obj. Saihello ("Tomas");

This syntax is the same as a normal method call, but there is an important difference. We also added another method call, but deliberately used a non-existent method name. This will compile correctly, because the name of the method is represented internally as a string, as in the previous example. The problem will not be discovered until run time. F # is a static type of fact note, and we don't have to worry about this: we can rely on the compiler to find similar errors when calling to other C # code.

When creating an F # Library to be used in C #, we need to differentiate between the two F # constructs. The first includes a class or record with a member, which appears as a standard C # class, with no trouble to use, and the second includes a value or higher-order function, compiled in a nonstandard way, and difficult to use in C #. Let's start with an example of the first case.

9.5 using the F # Library in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.