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 #