tags:

views:

370

answers:

5

In C# when I want to call a static method of a class from another static method of that class, is there a generic prefix that I can use such as PHP's self:: instead of the class name?

So in the below example, instead of saying Customer.DatabaseConnectionExists(), how can I say something like Self.DatabaseConnectionExists() so e.g. later if I change the name of the class I don't have to go change all the prefixes?

class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static Customer GetCurrentCustomer()
    {
        if (Customer.DatabaseConnectionExists())
        {
            return new Customer { FirstName = "Jim", LastName = "Smith" };
        }
        else
        {
            throw new Exception("Database connection does not exist.");
        }
    }

    public static bool DatabaseConnectionExists()
    {
        return true;
    }
}
A: 

No, there isn´t. But with the refactor tools, changing a name of a class should not worry you too much.

fbinder
+11  A: 

If you're calling the method from inside the class, you don't need to specify anything like ::Self, just the method name will do.

class Customer
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public static Customer GetCurrentCustomer()
    {
        if (DatabaseConnectionExists())
        {
            return new Customer { FirstName = "Jim", LastName = "Smith" };
        }
        else
        {
            throw new Exception("Database connection does not exist.");
        }
    }

    public static bool DatabaseConnectionExists()
    {
        return true;
    }
}
Kirschstein
I was writing the same answer + the fact that we could use a refactor tool in case we want to change the name of the class. Personally, I use "Resharper" refactoring tool.
Nordes
+6  A: 

There's no real equivalent - you have to either specify the class name, i.e.

Customer.DatabaseConnectionExists()

or miss out the qualifier altogether, i.e.

DatabaseConnectionExists()

The latter style of calling is advisable since it's simpler and doesn't lose any meaning. Also, it's more inline with method calling in instances (i.e. calling by InstanceMethod() and not this.InstanceMethod(), which is overly verbose).

Noldorin
I would think that it would make your code easier to read if you use "this." for instance methods and "ClassName." for static methods, otherwise you don't know by glancing at a method call if it is for an instance or static. Moving from PHP to C# I feel that I often have to look around in the context of the code to figure out this difference.
Edward Tanguay
@Edward Tanguay: I can see why you might think this, but really it's just being too verbose (at least in my opinion). You will very rarely see methods called using this.MethodName(), just because it's against most design guidelines. The usage of `this` with fields and properties, on the other hands, varies quite widely.
Noldorin
+3  A: 

Just leave it out. DatabaseConnectionExists is defined inside the class.

Dario
+1  A: 

Just call it without any prefix.

Alex Jenter