A Bit of Syntatic Sugar for C# Please

When I reference a method in C#, I always qualify it with its point of origin. Meaning if I call an instance method, I do this:

this.CallMe();

A static method is declared this way:

BigDescriptiveClassName.CallMe();

Now, I know that some people would say this is excessive but I think this makes the code more readable. But anyway...here's my point.

I'd love to be able to declare the static method this way:

class.CallMe();

There's a keyword for the instance reference...can I have a keyword for the class as well? "class", "static", whatever, I don't really care, just something that takes less keystrokes.

kthxbye :)

* Posted at 11.03.2008 09:38:30 AM (Last Update: 11.05.2008 07:38:30 AM) | 8 comments | Link | RSS *

Comments

# using Directive, from Keith Dahlby at 11.03.2008 11:30:25 AM

It's not quite as handy as PowerShell's :: syntax, but you can alias a class with the using directive:

using CA = Microsoft.SharePoint.Administration.SPAdministrationWebApplication;
class Program
{
static void Main(string[] args)
{
var ca = CA.Local;
}
}

# Another Example, from Keith Dahlby at 11.03.2008 11:47:23 AM

Or for your specific example:

using me = BigDescriptiveClassName;
class BigDescriptiveClassName
{
void DoCall() {
me.CallMe();
}

static void CallMe() { }
}

# Too Heavyweight, from Jason Bock at 11.03.2008 12:03:47 PM

Keith, I agree that "using" will work, but that feels too heavyweight. Having something like a keyword would be nicer.

# developer, from Jason Haley at 11.04.2008 09:38:10 AM

What about some sort of a short-cut or macro in the IDE that would take your 'class' and figure out that it should replace the 'class' with the type's name? That would be a nice shortcut ... still wouldn't be the syntax you are looking for though ... have to extend the compiler (or create precompiler)to make that one happen :)

# Ah, to have an extensible compiler..., from Jason Bock at 11.04.2008 12:33:50 PM

JasonH, Yeah, then I could do stuff like this :)

# this?, from Eric at 11.04.2008 12:34:38 PM

Why do you use the this qualifier? Do you think it makes your code easier to read? Is there another benefit? Do you use the this qualifier for class fields also?

# IMO more readable, from Jason Bock at 11.04.2008 12:36:54 PM

Eric, I think it's more readable to use "this". But for statics, I have to use the class name (no, I don't use "this" in this case).

# Static methods, from Skup at 11.05.2008 07:38:26 AM

Actually you only need to make the class name expicit for static calls when calling a method on another class.

Thus you can write : CallMe()
It is implicitly staticly called on you class.

Add a Comment

(*) = Required field
Name (*):

E-Mail (*):

Web Site:

Title (*):

Comments (*):

Enter the code you see (*)



Quote
"Nature knows no indecencies; man invents them." Mark Twain
Twitter History
follow me on Twitter
Blog History