Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
This article covers how you can get started with F# on any operating system (Windows, macOS, or Linux) with the .NET CLI. It goes through building a multi-project solution with a class library that is called by a console application.
Prerequisites
To begin, you must install the latest .NET SDK.
This article assumes that you know how to use a command line and have a preferred text editor. If you don't already use it, Visual Studio Code is a great option as a text editor for F#.
Build a simple multi-project solution
Open a command prompt/terminal and use the dotnet new command to create a new solution file called FSharpSample:
dotnet new sln -o FSharpSample
The following directory structure is produced after running the previous command:
FSharpSample
├── FSharpSample.sln
Write a class library
Change directories to FSharpSample.
Use the dotnet new command to create a class library project in the src folder named Library.
dotnet new classlib -lang "F#" -o src/Library
The following directory structure is produced after running the previous command:
└── FSharpSample
├── FSharpSample.sln
└── src
└── Library
├── Library.fs
└── Library.fsproj
Replace the contents of Library.fs with the following code:
module Library
open System.Text.Json
let getJson value =
let json = JsonSerializer.Serialize(value)
value, json
Add the Library project to the FSharpSample solution using the dotnet sln add command. This command adds the project to the solution file so that the solution can track and build it:
dotnet sln add src/Library/Library.fsproj
Run dotnet build to build the project. Unresolved dependencies will be restored when building.
Write a console application that consumes the class library
Use the dotnet new command to create a console application in the src folder named App.
dotnet new console -lang "F#" -o src/App
The following directory structure is produced after running the previous command:
└── FSharpSample
├── FSharpSample.sln
└── src
├── App
│ ├── App.fsproj
│ ├── Program.fs
└── Library
├── Library.fs
└── Library.fsproj
Replace the contents of the Program.fs file with the following code:
open System
open Library
[<EntryPoint>]
let main args =
printfn "Nice command-line arguments! Here's what System.Text.Json has to say about them:"
let value, json = getJson {| args=args; year=System.DateTime.Now.Year |}
printfn $"Input: %0A{value}"
printfn $"Output: %s{json}"
0 // return an integer exit code
Add a reference to the Library project using dotnet reference add. This command adds a <ProjectReference> element to the App.fsproj file, which tells the compiler that the App project depends on the Library project:
dotnet add src/App/App.fsproj reference src/Library/Library.fsproj
The previous command adds the following XML to the App.fsproj file:
<ItemGroup>
<ProjectReference Include="..\Library\Library.fsproj" />
</ItemGroup>
Tip
If you skip this step and try to build the App project, you'll get a compilation error because the Library module won't be found. If this happens, you can either run the dotnet add reference command or manually add the <ProjectReference> element shown above to your App.fsproj file.
Add the App project to the FSharpSample solution using the dotnet sln add command:
dotnet sln add src/App/App.fsproj
Restore the NuGet dependencies with dotnet restore and run dotnet build to build the project.
Change directory to the src/App console project and run the project passing Hello World as arguments:
cd src/App
dotnet run Hello World
You should see the following results:
Nice command-line arguments! Here's what System.Text.Json has to say about them:
Input: { args = [|"Hello"; "World"|] year = 2021 }
Output: {"args":["Hello","World"],"year":2021}
Next steps
Next, check out the Tour of F# to learn more about different F# features.