Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use C# object from F#?

Tags:

c#

f#

I have the following C# code.

namespace MyMath {
    public class Arith {
        public Arith() {}
        public int Add(int x, int y) {
            return x + y;
        }
    }
}

And I came up with the F# code named testcs.fs to use this object.

open MyMath.Arith
let x = Add(10,20)

When I run the following command

fsc -r:MyMath.dll testcs.fs

I got this error message.

/Users/smcho/Desktop/cs/namespace/testcs.fs(1,13): error FS0039: The namespace 'Arith' is 
not defined

/Users/smcho/Desktop/cs/namespace/testcs.fs(3,9): error FS0039: The value or constructor 
'Add' is not defined

What might be wrong? I used mono for .NET environment.

like image 202
prosseek Avatar asked Oct 10 '10 19:10

prosseek


1 Answers

try

open MyMath
let arith = Arith() // create instance of Arith
let x = arith.Add(10, 20) // call method Add

Arith in your code is class name, you cannot open it like namespace. Possibly you are confused with ability to open F# modules so its functions can be used without qualification

like image 81
desco Avatar answered Oct 14 '22 00:10

desco