Click here to Skip to main content
Licence 
First Posted 22 Sep 2001
Views 106,765
Bookmarked 12 times

A C# idiom to simulate global functions

By | 22 Sep 2001 | Article
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

About the Author

another

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionWhy should someone use it? PinmemberLeonardo Pires6:52 26 Oct '07  
GeneralStatic Members and Thread-safety PinsussSimon Tocker5:40 18 Mar '04  
Generalproblem deleting assembly from gac /uf doesnot work Pinmembersanapk12:30 10 Apr '03  
QuestionWhat's wrong with static class methods? PinmemberJohn Rayner13:24 29 Apr '02  
AnswerRe: What's wrong with static class methods? PinmemberCraig Dodge16:24 29 Apr '02  
GeneralCool stuff but static methods are better PinmemberAlvaro Mendez6:14 24 Sep '01  
GeneralRe: Cool stuff but static methods are better PinmemberMike Klimentiev10:40 24 Sep '01  
GeneralRe: Cool stuff but static methods are better PinmemberCraig Dodge12:53 24 Sep '01  
GeneralRe: Cool stuff but static methods are better PinmemberMike Klimentiev10:32 25 Sep '01  
GeneralRe: Cool stuff but static methods are better PinmemberAlvaro Mendez10:37 25 Sep '01  
GeneralRe: Cool stuff but static methods are better PinmemberMike Klimentiev11:39 25 Sep '01  
GeneralRe: Cool stuff but static methods are better PinmemberJeff Varszegi4:44 20 Mar '04  
GeneralRe: Cool stuff but static methods are better PinsussAnonymous3:06 18 Feb '04  
GeneralRe: Cool stuff but static methods are better PinmemberJeff Varszegi5:58 18 Mar '04  
QuestionStatic members? PinmemberNick Blumhardt20:12 23 Sep '01  
AnswerRe: Static members? PinmemberCraig Dodge7:36 24 Sep '01  
GeneralRe: Static members? PinmemberMike Burston11:40 24 Sep '01  
GeneralRe: Static members? PinmemberCraig Dodge12:40 24 Sep '01  
GeneralRe: Static members? PinmemberSai7:39 25 Sep '01  
GeneralRe: Static members? PinmemberMike Klimentiev10:35 25 Sep '01  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 23 Sep 2001
Article Copyright 2001 by another
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid