19

Why can't we use F# class within a C# project?

Eventually, it's all CIL, is it not?

Is there any reason we can't add an F# source file to a C# project then?

Letterman
  • 4,076
  • 5
  • 29
  • 41
  • 7
    Why do you think you can't? – Oded Jan 11 '13 at 14:18
  • you can use c# from f# reverse should be fine http://stackoverflow.com/questions/5731693/using-c-sharp-library-in-an-f-project – adt Jan 11 '13 at 14:19
  • 3
    For the same reason you can't mix C++\C#, or C#\VB, or IronPython\C# in the same project. What's the compiler supposed to do with the file? – Wesley Wiser Jan 11 '13 at 14:20
  • 2
    In addition to hvd's answer, if you truly feel that Microsoft should dedicate resources to creating a new project type that can handle multiple compilers, you can post your suggestion on the Microsoft Connect site, and if enough people agree with you then they will vote it up, perhaps enough that Microsoft does implement a way to handle this exact situation (mixed source files). – myermian Jan 11 '13 at 15:00
  • 6
    "What's the compiler supposed to do with the file?" It's supposed to read the file extension! :) – Andres Jaan Tack Jun 17 '13 at 08:23

2 Answers2

36

You can use an F# class in a C# project. You cannot use a F# source file in a C# project.

C# projects are compiled by the C# compiler. F# projects are compiled by the F# compiler. In order to use an F# class in a C# project, you need to create a separate F# project, put your class in there, and add a reference to that project in your C# project.

7

In order to use assemblies from different languages, you need to ensure that your code is CLS compliant.

This ensures that your code uses features that are common and compatible with all .NET languages.


You cannot mix code files of different languages in one project. That's a limitation of Visual Studio and how its build system has been designed.

Oded
  • 489,969
  • 99
  • 883
  • 1,009