16

I know you can use C# and F# together in the same project however I'm not sure if its a good idea to do so.

It seems to me that mixing two very different coding styles (functional vs OOP) could cause a lack of cohesion in the design. Is this correct?

wonea
  • 141
  • 7
Tom Squires
  • 17,695
  • 11
  • 67
  • 88
  • 1
    How can you use them in the same project? Do you mean the same solution? – Bryan Boettcher Feb 23 '12 at 15:53
  • 2
    Not the Microsoft Definition of project but the more general definition – Tom Squires Feb 23 '12 at 16:05
  • One obvious drawback is that if at least part of the work on the project requires that the developer interact with both sections in C# and in F# (even just to read them), those parts will require a developer knowledgeable in both languages, and there are less of them than those knowledgeable in just one language, and thus they are harder to find, potentially more expensive and there's less variety of them (in this specific case though it's likely that most F# developers have at least some familiarity with C# as well). – gbr Nov 01 '17 at 19:28
  • absolutely. e.g. gradually introducing F# into a legacy C# solution is a valid scenario. – KolA Aug 21 '19 at 09:15

3 Answers3

23

There's nothing wrong with mixing languages in a product as long as you use each appropriately and they "play nice" together.

If there is part of your project that would be best coded using a functional language then it makes sense to code it in F#. Similarly for C#.

What would be pointless (at best) would be mixing languages for the sake of it.

ChrisF
  • 38,878
  • 11
  • 125
  • 168
0

Yes I agree with ChrisF. Also C# currently already incorporates F# principles, such as anonymous types:

var unitanon = new[]
{
    new { Field1="new car", Field2 = 1},
    new { Field1="old car", Field2 = 20} 
};

That said, I personally think it's a step backward in code readability, though it is very convenient.

wonea
  • 141
  • 7
  • 2
    Anonymous types are not really an F# principle. May be you mean to refer to type inference (what the **var** keyword does in C#) whereby the compiler guesses what type you are referring to. This is used throughout F#. – Mongus Pong Nov 08 '11 at 11:14
  • 1
    I would use F# for the easy async capabilities. However, the TPL has made it quite easy to do that in C# by itself. If you were to embed a language for scripting though, I would definitely use F# for the parsing of that. – Jetti Dec 07 '11 at 17:33
0

It seems to me that mixing two very different coding styles (functional vs OO) could cause a lack of cohesion in the design. Is that correct?

I don't think you'll get a "lack of cohesion". Each language has strengths and weaknesses. Combining them on a common-language run-time lets you get closer to the best of both worlds. With C# and F#, you just want to make sure you use the intersection of the two languages at the interface between the two in your solution.

J D
  • 2,332
  • 24
  • 17