MatthewMartin
MatthewMartin

Reputation: 33193

Can I get the same benefits of functional programming (F#) by using more static methods in C#?

I admit I haven't grokked F# yet. But in the 30,000 foot descriptions, they keep talking about easy to test code that doesn't have mutable state. Is that the same as static methods?

Could I get the benefit of F# by writing some of my code in classes with all static methods?

I'm just looking for the short answer, I know whole books exist on the topic.

Upvotes: 7

Views: 565

Answers (7)

Ben Griswold
Ben Griswold

Reputation: 18381

IT's true. You aren't going to get the benefits of functional programming just by using more static functions in C#. Howver, if you were to look under the hood (using Reflector, for example) I understand a simple let statement is a static function. In other words,

//F#
let a = 2

is like a function in C#

//C#
static int a()
{
    return 2;
}

I can understand the confusion.

Explanation pulled from Leon Bambrick's "F# Eye for the C# Guy presentation."

Upvotes: 1

Benjol
Benjol

Reputation: 66637

I beg to differ with all the other answers to date. Immutability and static methods may not be strictly technically related, but I have found that using F# has encouraged me to make C# methods static whenever I can.

The thinking is analogue, in that it is easier to handle an immutable object because you don't have to worry about it changing state. In the same way, you don't have to worry about state when using a static method (unless you use a global singleton or something...).

Upvotes: 4

MichaelGG
MichaelGG

Reputation: 10006

Apart from static, functional modules versus objects, you can attempt to get some of the benefits of F# by using C# 3 and lambdas, LINQ, etc. However, that doesn't go very far. What I find nice in F# is:

  • Inference everywhere
  • Auto-generalization (adds in type parameters so I don't have to sort it out manually)
  • Easy immutability
  • Easy mix between module and classes
  • Types like discriminated unions
  • Pattern matching
  • Nested functions (lightweight)
  • First class functions (no, C#'s named delegates don't count)
  • Everything's an expression
  • Easy function composition

So, you can try to do some of this in C#. Some of it is simply not supported and won't work. The rest gets very ugly, very fast.

So, if you go much off the beaten path of LINQ and the Enumerable extensions, you'll probably end up in a world of pain.

Upvotes: 5

Adam Luter
Adam Luter

Reputation: 2253

You could certainly write C# code immutably, but that's nothing to do with static functions. Immutability are things like having a struct or object that only "changes state" by making a copy of itself and having the copy be different.

Upvotes: 8

Jeromy Irvine
Jeromy Irvine

Reputation: 11814

No, the two concepts are unrelated. Static methods in C# can still modify incoming objects as normal, and other variables using ref or out.

Upvotes: 2

Anton Gogolev
Anton Gogolev

Reputation: 115897

Absolutely no, immutability has nothing to do with methods being static or instance. String, being an immutable class, has plenty of instance methods, which, in a very functional manner, return a new instance of String class, rather than modifying anything.

You could try to emulate F# by using functional decomposition, but this will still be pretty imperative code.

Upvotes: 8

Anton Tykhyy
Anton Tykhyy

Reputation: 20086

No, it's not the same as static methods. You don't have mutable state if you don't assign anything (locals, function arguments, static fields, instance fields) after it was initialized. You can get some of the benefits by designing your classes to be immutable.

Upvotes: 3

Related Questions