# How to use a C# library from F#?

**URL:** <https://forums.fsharp.org/t/how-to-use-a-c-library-from-f/3495>\
**Category:** General\
**Created:** [January 2, 2024, 8:13pm UTC](https://forums.fsharp.org/t/how-to-use-a-c-library-from-f/3495 "2024-01-02T20:13:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![curioustolearn](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/curioustolearn/32/1278_2.png) [@curioustolearn](https://forums.fsharp.org/u/curioustolearn)\
**Post date:** [January 2, 2024, 8:13pm UTC](https://forums.fsharp.org/t/how-to-use-a-c-library-from-f/3495/1 "2024-01-02T20:13:46Z")

</div>

Hello,

I am new to F# (and .Net). Never programmed in C# or on dotnet. Just started learning F# about a month or so back to explore using it, since it can be now used on Mac and Linux.

I was trying to see if I can use a C# library, [Cocona](https://github.com/mayuki/Cocona) from F#.  
For now I am wondering how to translate the following code example from the Github page of the library to F#.

```auto
var builder = CoconaApp.CreateBuilder();
var app = builder.Build();

app.AddCommand((string name) => { 
     Console.WriteLine($"Hello {name}");
});

app.Run();

```

I created an F# project and added `CoconaLite` to the project using `dotnet add` command. Then I typed the following:

```auto
let builder = CoconaLiteApp.CreateBuilder();
let app = builder.Build();

app.AddCommand (fun (name: string) -> printfn "My name is {name}." |> ignore)

app.Run()

```

If I try to do this, I get he following error:

```auto
error FS0041: No overloads match for method 'AddCommand'.Known type of argument:
(string -> unit)Available overloads: - (extension)
Builder.ICoconaCommandsBuilder.AddCommand(commandBody: Delegate) :
Builder.CommandConventionBuilder // Argument 'commandBody' doesn't match -
(extension) Builder.ICoconaCommandsBuilder.AddCommand(commandData:
Builder.ICommandData) : Builder.ICoconaCommandsBuilder // Argument 'commandData'
doesn't match
[/Users/curioustolearn/LearnSoftware_and_Math/LearnFsharp/App1/App1.fsproj]

The build failed. Fix the build errors and run again.
The build failed. Fix the build errors and run again.

```

Can someone please let me know if it is possible to use a C# library like Cocona from F#, and a simple example of how to achieve what I am trying to do above. Any link to a resource or example that helps with this question would be helpful too.

Thank you very much.

---

<div class="post-metadata">

**Author:** ![altbodhi](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/altbodhi/32/1253_2.png) [@altbodhi](https://forums.fsharp.org/u/altbodhi)\
**Post date:** [January 3, 2024, 12:14pm UTC](https://forums.fsharp.org/t/how-to-use-a-c-library-from-f/3495/2 "2024-01-03T12:14:05Z")

</div>

Hello! F# language with strong explicit cast types.  
And in your case you only need provide true type for c# method (Delegate)

```auto

let cmd = System.Action<_>(fun (name: string) -> printfn "My name is {name}.")

app.AddCommand cmd |> ignore

```

---

<div class="post-metadata">

**Author:** ![curioustolearn](https://yyz2.discourse-cdn.com/flex030/user_avatar/forums.fsharp.org/curioustolearn/32/1278_2.png) [@curioustolearn](https://forums.fsharp.org/u/curioustolearn)\
**Post date:** [January 3, 2024, 1:52pm UTC](https://forums.fsharp.org/t/how-to-use-a-c-library-from-f/3495/3 "2024-01-03T13:52:49Z")

</div>

Thank you so much @altbodhi. This worked great. Can you please point me to some resources where I can learn more about this? How did you know to use System.Action, or what the type for the C# method is? I don’t know C#. Do I need to learn that for this kind of stuff? Does any book or online docs on F# cover this?

Thank you again.
