Click here to Skip to main content
15,887,746 members
Articles / Programming Languages / C#
Article

A C# idiom to simulate global functions

Rate me:
Please Sign up or sign in to vote.
1.00/5 (16 votes)
22 Sep 20011 min read 125.9K   372   12   20
When what you really want is a global function.

Introduction

Recently I was working on a project using C# when I discovered I had several classes that needed to query the registry to return a string containing a path. The function was non-trivial, so a simple cut and paste would invite maintenance bugs. If I were working in C++, I would simply create a global function, and the classes could call that function. But C# doesn't allow for global functions. I considered:

class foo
{
	public GetString(){ //...
	}
}

then each class would do the following:

foo bar = new foo();
string WhatIReallyWant = bar.GetString();

but that involves creating a new variable, and I thought it was just plain ugly. Then I found a way to simulate having a global function without actually having one. Here is how you do it:

  • Create a class with a name of the "function" you want to call.
  • Give it a member variable of the type you want to return.
  • Create a constructor with the same arguments you want to pass in.  In this constructor, do what you want your function to do and assign the result to the variable you created above.
  • Overload the implicit cast operator for the type you want. Have it return the variable.

Then, in your code, preface the calls to this "function" with the new operator.

Here is the relevant code from the demo project. Here is a simple example that reverses a string: 

class reverser
{
	private string Data;
	public reverser(string toReverse)
	{
		for (int i=toReverse.Length-1; i>=0; i--)
		{
			Data += toReverse[i];
		}
	}

	public static implicit operator string(reverser r)
	{
		return r.Data;
	}
}

As you can see, the name of the "function" is reverser, the variable is named Data, it takes a string argument toReverse, and then we overload the cast to string operator. The casting operator is defined as implicit so we don't have to preface every call with a (string). Then you can call the "function" like this:

string foo = new reverser("foobar");

Which looks almost like a regular function call, except for the new operator.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhy should someone use it? Pin
Leonardo Pires26-Oct-07 6:52
Leonardo Pires26-Oct-07 6:52 
GeneralStatic Members and Thread-safety Pin
Simon Tocker18-Mar-04 5:40
sussSimon Tocker18-Mar-04 5:40 
Generalproblem deleting assembly from gac /uf doesnot work Pin
sanapk10-Apr-03 12:30
sanapk10-Apr-03 12:30 
QuestionWhat's wrong with static class methods? Pin
John Rayner29-Apr-02 13:24
John Rayner29-Apr-02 13:24 
AnswerRe: What's wrong with static class methods? Pin
another29-Apr-02 16:24
another29-Apr-02 16:24 
GeneralCool stuff but static methods are better Pin
Alvaro Mendez24-Sep-01 6:14
Alvaro Mendez24-Sep-01 6:14 
GeneralRe: Cool stuff but static methods are better Pin
Mike Klimentiev24-Sep-01 10:40
Mike Klimentiev24-Sep-01 10:40 
GeneralRe: Cool stuff but static methods are better Pin
another24-Sep-01 12:53
another24-Sep-01 12:53 
GeneralRe: Cool stuff but static methods are better Pin
Mike Klimentiev25-Sep-01 10:32
Mike Klimentiev25-Sep-01 10:32 
GeneralRe: Cool stuff but static methods are better Pin
Alvaro Mendez25-Sep-01 10:37
Alvaro Mendez25-Sep-01 10:37 
GeneralRe: Cool stuff but static methods are better Pin
Mike Klimentiev25-Sep-01 11:39
Mike Klimentiev25-Sep-01 11:39 
GeneralRe: Cool stuff but static methods are better Pin
Jeff Varszegi20-Mar-04 4:44
professionalJeff Varszegi20-Mar-04 4:44 
GeneralRe: Cool stuff but static methods are better Pin
Anonymous18-Feb-04 3:06
Anonymous18-Feb-04 3:06 
GeneralRe: Cool stuff but static methods are better Pin
Jeff Varszegi18-Mar-04 5:58
professionalJeff Varszegi18-Mar-04 5:58 
QuestionStatic members? Pin
Nick Blumhardt23-Sep-01 20:12
Nick Blumhardt23-Sep-01 20:12 
AnswerRe: Static members? Pin
another24-Sep-01 7:36
another24-Sep-01 7:36 
GeneralRe: Static members? Pin
Mike Burston24-Sep-01 11:40
Mike Burston24-Sep-01 11:40 
GeneralRe: Static members? Pin
another24-Sep-01 12:40
another24-Sep-01 12:40 
GeneralRe: Static members? Pin
25-Sep-01 7:39
suss25-Sep-01 7:39 
GeneralRe: Static members? Pin
Mike Klimentiev25-Sep-01 10:35
Mike Klimentiev25-Sep-01 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.