6

Being a C++ programmer, every time I work with C# I wonder why it lacks support for freestanding functions; in other words: functions that are not part of any class. I really miss this feature, because standalone functions allow to add functionality to classes without requiring full private access, avoiding hard-to-maintain monolith classes. Furthermore, it allows extending third party libraries. I know you can use a static class, but the class name is totally irrelevant, making the client code unnecessarily verbose.

For example. I want to create a helper function to count the words in a string. How can I avoid having to write "StringHelperClass.CountWords();" in the client code? StringHelperClass acts as a namespace, only I cannot write "using StringHelperClass;". I am forced to repeat "StringHelperClass" with every usage, whereas it is obvious that it is a string helper function, as its only parameter is a string.

Is there a way to extend a class' functionality, while keeping the client code concise?

Dimitri C.
  • 21,861
  • 21
  • 85
  • 101
  • Yeah, I see no evidence of a question here. – Noldorin May 18 '09 at 10:42
  • 1
    I voted to close: "not a real question". – Andrew Hare May 18 '09 at 10:47
  • What sense does a method outside of a class make in an OO language? – annakata May 18 '09 at 10:49
  • 2
    Indeed, C# is only being properly OO by not allowing freestanding functions. – Noldorin May 18 '09 at 10:50
  • 1
    Edit the post to turn it into a question and I'll happily vote to re-open. There's not a single question mark in either the title or the post! – Robin Day May 18 '09 at 10:50
  • 2
    "Every new version of C# I expect this feature to be added" -- why? Did I or anyone else on the design team ever say anything to lead you to believe that we were considering adding this feature? "for some unclear reason the designers don't find it important" -- really? Are you using your ESP powers to figure out what I'm thinking? I don't recall seeing you at any of the several meetings where we discussed this possible feature, so I'm not sure where you're getting the idea that we don't think it's important. – Eric Lippert May 18 '09 at 19:33
  • Hello mister Lippert. I didn't expect to get a personal response of one of the C# designers :-) I must admit my question/remark was a little provocative; I'll rewrite it in a more neutral way. The thing is, I am a typical user, only seeing the end result, ignoring all other design tradeoffs. I always compare C# to C++ and find it hard to accept that a newer programming language throws out some of the (in my opinion) basic features, such as const-correctness, pointers guaranteed to be not null, and freestanding functions. However, the "extension methods" feature largely solves my problem. – Dimitri C. May 19 '09 at 10:00
  • Just by the way, there is another similar question on SO that has a great answer by Eric Lippert explaining it a bit more: http://stackoverflow.com/questions/1024171/why-c-is-not-allowing-non-member-functions-like-c – Michael Stum Aug 27 '09 at 11:19

2 Answers2

9

Edit: note that since the using static .... directive was added in C# 6, this can largely be considered an equivalent feature. Just use regular .NET static methods, then use a using static directive, and those methods are immediately available to you.


The better question would be:

"Why should it include it?"

Given that C# holds together pretty well without freestanding functions, what is the requirement to add them? In particular, static utility classes work fine for hosting such (either as regular or extension methods), and allow simple disambiguation when conflicts arise.

It also (as OregonGhost notes in the comments) helps modularize the code, preventing death-by-IntelliSense etc.

So: why add them? What is the problem (in C#) that isn't already solved?

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 3
    That's so true. IMO static utility classes actually add value to the code, because they force you to modularize the code, and that's always a good thing. Of course, that can be misused, but that's true for free-floating functions as well. Oh, and extension methods make things even better. For example, why should a string function be free-floating when it can be added to the string class? :) – OregonGhost May 18 '09 at 10:43
3

I agree that it's a feature I find lacking in C#. The main reason is verbosity of using the functions. The class name acts as a namespace, but there's no way of simply 'using' that namespace at the start of a code file. So for example if you want to write OpenGL in C# you would have to type things like: OpenGL.glClearDepth(1.0f); OpenGL.glEnable(GL_DEPTH_TEST);, etc. I know it probably has something to do with the basic concept of making everything a class, but I don't really like languages that force you to do things a certain way. Of course that's maybe the reason I don't really use C# unless I have no other choice (like at work).

Randy Voet
  • 3,670
  • 4
  • 29
  • 36
  • 1
    Arguably, those method names are named with redundancy - it could be just `OpenGL.ClearDepth(1.0f);` - and of course, if you *really* want you could use an alias declaration, so `GL.ClearDepth(1.0f)` - which still has all the advantages I mentioned (unless you find a "." offensive). – Marc Gravell May 18 '09 at 11:04
  • "The class name acts as a namespace" Really?? So all the overhed of VMT just for a namespace? OMG. – Mihaela May 02 '18 at 22:50
  • @Mihaela I've added an edit to my answer; in more recent versions of C#, `using static` essentially "solves" this; with a `using static OpenGL;`, then `glClearDepth` and `glEnable` would be immediately available (to use the examples from this answer) – Marc Gravell May 02 '18 at 23:08