Click here to Skip to main content
15,898,035 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 
Hi Guys,
I have just recently started to work in the dot net arena.
So as I was trying different examples I installed an assembly in the GAC by a simple drag and drop approach to the winnt\assembly directory. I kind of forgot about this assembly for a while as I went on trying some other things, I later fixed the version of my assembly to 1.0.0.0. and recompiled it many times over as I continued with my experiments.

The main problem I am facing now is that I am unable to delete this assembly from the gac.

Here are the outputs I am getting

C:\>gacutil /lr ExampleDll

Microsoft (R) .NET Global Assembly Cache Utility. Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

The Global Assembly Cache contains the following assemblies:
ExampleDll, Version=1.0.1188.31895, Culture=neutral, PublicKeyToken=c73a3aadb8
8be979, Custom=null
SCHEME: <WINDOWS_INSTALLER> ID: <MSI> DESCRIPTION : <Windows Installer>

The cache of ngen files contains the following entries:

Number of items = 1

C:\>gacutil /uf ExampleDll

Microsoft (R) .NET Global Assembly Cache Utility. Version 1.0.3705.0
Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.


Assembly: ExampleDll, Version=1.0.1188.31895, Culture=neutral, PublicKeyToken=c73a3aad
b88be979, Custom=null
Assembly could not be uninstalled because it is required by Windows Installer
Unable to uninstall: assembly is required by one or more applications
Pending references:
SCHEME: <WINDOWS_INSTALLER> ID: <MSI> DESCRIPTION : <Windows Installer>

Number of items uninstalled = 0
Number of failures = 0


I don’t how windows installer added a reference to this assembly.
Any idea how I could delete this assembly from gac.

I found this link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/msi/setup/removal_of_assemblies_from_the_global_assembly_cache.asp

but still have not been able to figure out how to delete/uninstall the dll


thanks in advance

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.