Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Get started with F# in Visual Studio

Feedback

In this article

F# is supported in the Visual Studio integrated development environment (IDE).

To begin, ensure that you haveVisual Studio installed with F# support.

Create a console application

One of the most basic projects in Visual Studio is the console app. Here's how to create one:

  1. Open Visual Studio 2019.

  2. On the start window, chooseCreate a new project.

  3. On theCreate a new project page, chooseF# from the Language list.

  4. Choose theConsole App (.NET Core) template, and then chooseNext.

  5. On theConfigure your new project page, enter a name in theProject name box. Then, chooseCreate.

    Visual Studio creates the new F# project. You can see it in the Solution Explorer window.

Write the code

Let's get started by writing some code. Make sure that theProgram.fs file is open, and then replace its contents with the following:

module HelloSquarelet square x = x * x[<EntryPoint>]let main argv =    printfn "%d squared is: %d!" 12 (square 12)    0 // Return an integer exit code

The previous code sample defines a function calledsquare that takes an input namedx and multiplies it by itself. Because F# usesType inference, the type ofx doesn't need to be specified. The F# compiler understands the types where multiplication is valid and assigns a type tox based on howsquare is called. If you hover oversquare, you should see the following:

val square: x: int -> int

This is what is known as the function's type signature. It can be read like this: "Square is a function that takes an integer named x and produces an integer". The compiler gavesquare theint type for now.

Another function,main, is defined, which is decorated with theEntryPoint attribute. This attribute tells the F# compiler that program execution should start there. It follows the same convention as otherC-style programming languages, where command-line arguments can be passed to this function, and an integer code is returned (typically0).

It is in the entry point function,main, that you call thesquare function with an argument of12. The F# compiler then assigns the type ofsquare to beint -> int (that is, a function that takes anint and produces anint). The call toprintfn is a formatted printing function that uses a format string and prints the result (and a new line). The format string, similar to C-style programming languages, has parameters (%d) that correspond to the arguments that are passed to it, in this case,12 and(square 12).

Run the code

You can run the code and see the results by pressingCtrl+F5. Alternatively, you can choose theDebug >Start Without Debugging from the top-level menu bar. This runs the program without debugging.

The following output prints to the console window that Visual Studio opened:

12 squared is: 144!

Congratulations! You've created your first F# project in Visual Studio, written an F# function that calculates and prints a value, and run the project to see the results.

Next steps

If you haven't already, check out theTour of F#, which covers some of the core features of F#. It provides an overview of some of the capabilities of F# and ample code samples that you can copy into Visual Studio and run.

See also

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?