3

Possible Duplicate:
Why C# is not allowing non-member functions like C++

Instead of writing StaticClass.Function() I'd like to simply write Function(). There will be many functions and all should be accessible from different (and unrelated) classes and files. How do I put these functions in a specific namespace? Simply declaring it there will give me a compile error

error CS1518: Expected class, delegate, enum, interface, or struct

I know other .NET languages can do it. Is there a compile option i may use? Perhaps even undocumented?

Community
  • 1
  • 1
  • AFAIK, there's no way to define a "global" method, though you could write Function() inside the class itself. There's really no need, though.. – Daniel Aug 09 '12 at 23:25
  • 1
    BTW, you don't want to do this. They _can_ be accessed from various classes, you just have to say which class. – John Saunders Aug 09 '12 at 23:25
  • 1
    @JohnSaunders I do really want to do this. All the functions are neatly placed in a namespace and i'd like to access them if i include the namespace. I can't possibly think of why C# would allow gotos and not this. Really... –  Aug 09 '12 at 23:58
  • ugh, not worth the time to implement is what the article said. I guess that answers it. thanks @EdS. –  Aug 10 '12 at 00:00

3 Answers3

10

C# does not allow for free functons. Each function must reside in a type. This is just the way it works, it's not a matter of technical possibility, it was a design decision.

You may be interested in this article.


On a side note, ever notice how Intellisense works much, much better when writing C# than C++? This is one of those things that help (not the only one, but one).

EDIT: Funny, in reading that linked article I noticed that this is a dup...

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • FYI, VS2012 has a bunch of C++ editor/IntelliSense improvements. :) – Chris Sinclair Aug 10 '12 at 00:27
  • @ChrisSinclair: Yes, it does. Actually, even 2010 was a *huge* step forward in terms of C++ intellisense (and a huge step *backward* in terms of performance, but that is neither hear nor there...). C++ Intellisense in 2005 was a joke, never used 2008. So yeah, it's getting better, but it is so much easier to implement for a language like C#. – Ed S. Aug 10 '12 at 00:29
2

C# does not allow this, by design.

However, if your goal is merely to reduce typing, you have a couple of options.

First, you can use the using Directive to simplify this. By adding this:

using SC = YourNamespace.StaticClass;

You can shorten the calls within that specific document to:

SC.Function();

Another option which is occasionally appropriate would be to use an Extension method. This can eliminate the need to specify the type, as the function appears to be a member function of the first argument. Of course, this wouldn't work for the supplied example (as it requires a parameter), but is potentially another option to reduce the amount of typing and searching, depending on the specific use case.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
-2

C# is a purely object-oriented which means you cannot have functions or declarations outside of a class. You'll have to use static to achieve what you want.

DelegateX
  • 719
  • 3
  • 8
  • 1
    -1 OO doesn't mandate that all functions belong to a class. Actually, it doesn't mandate that "classes" even exist. Classes are simply one way of implementing OOP (data structures of *some* sort are of course required). Also, Eric Lipper doesn't mention this *at all* when explaining the decision. (he does mention however that "C# is a *component*-oriented language") – Ed S. Aug 09 '12 at 23:30