Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can global function exist in C#?

How can global function exist in C# when everything is defined inside a class? I was reading the documentation of OpCodes.Call at MSDN, and was surprised to see the following wordings,

The metadata token carries sufficient information to determine whether the call is to a static method, an instance method, a virtual method, or a global function.

Global function? Does it exist in C#? (It definitely doesn't refer to static method, as it's explicitly listed along with global function).

like image 981
Nawaz Avatar asked Sep 03 '11 21:09

Nawaz


People also ask

Are all functions global in C?

Are all functions in c global? No. For one thing, what many call (sloppily) global, the C Language calls file scope with external linkage.

How do you make a global function?

The global Keyword Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function. To create a global variable inside a function, you can use the global keyword.

How do you define a global function?

If you define functions in . pdbrc they will only be available from the stack frame from which pdb. set_trace() was called. However, you can add a function globally, so that it will be available in all stack frames, by using an alternative syntax: def cow(): print("I'm a cow") globals()['cow']=cow.

What is the difference between a global function and a local function?

Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Local variables are declared inside a function, and can be used only inside that function. It is possible to have local variables with the same name in different functions.


2 Answers

You can't have global functions in C#, it's just not part of the language. You have to use a static method on some class of your choosing to get similar functionality.

However C# is not the only language that uses the CLR. One can write Managed C++, which can have global functions.

like image 142
i_am_jorf Avatar answered Sep 17 '22 23:09

i_am_jorf


At the Build 2014 conference it was announced that from Roslyn onwards, you can import static methods from types by the using TypeName; directive, so that instead of having to use System.Math.Min(...) you can do:

using System.Math;
...
var z = Min(x,y);

Note: by the time of release this became:

using static System.Math;
like image 28
Marc Gravell Avatar answered Sep 19 '22 23:09

Marc Gravell