Accessing static members with class or struct

Topics: C# Language Design
May 13, 2014 at 4:33 PM
We can use the word this when we want to browse for member methods/properties/fields on the same instance of a class/struct, but when we want to do the same for static members on the same class/struct we have to use the name of that class/struct which can be a pain when the name of the class/struct is rather long.
public class RatherLongNameOfClass
{
  ...

  public static void SomeStaticMethod()
  {
    ...
  }

  ...
  
  public void SomeMethod()
  {
    ...

    RatherLongNameOfClass.SomeStaticMethod();

    ...
  }
  
  ...
}
Thus, my proposal is to use the words class/struct instead of its name.
public class RatherLongNameOfClass
{
  ...

  public static void SomeStaticMethod()
  {
    ...
  }

  ...

  public void SomeMethod()
  {
    ...

    class.SomeStaticMethod(); // or struct.SomeStaticMethod() for the case of structs

    ...
  }
  
  ...
}
Ditto for constants.
May 13, 2014 at 4:58 PM
Considering that the identifier is entirely optional that sounds like a language feature specifically to drive IDE Intellisense?
May 13, 2014 at 6:12 PM
Halo_Four wrote:
Considering that the identifier is entirely optional that sounds like a language feature specifically to drive IDE Intellisense?
The ability to use a short "identifier" to refer to the current class could be useful in a few other situations as well, such as when the combination of a class name and its generic arguments gets unwieldy, or when it is necessary to have two classes with almost identical code, each of which should contain references to its own type [e.g. for purposes of "competitive" benchmarking].

As for the identifier being optional, there seem to be some quirks. If a class defines static and instance method overloads with the same name, overload resolution from outside the class will look only at the static ones or only at the instance ones, depending upon whether a member of the class or instance is being accessed, but that doesn't seem to work within a class. At attempt to access this.Foo(1,2,3) will fail if a static overload would be a better match for the parameters. Likewise an attempt to access classOfThis.Foo(1.0f,2,3); will fail if an instance method would be a better match. It seems curious that code outside the class has no trouble requesting a static or instance overload, but code inside the class can't.
May 14, 2014 at 5:13 AM
Unlikely worthy. You can create alias using MyClass = RatherLongNameOfClass; and it'll work for everything but constructors'/destructors' names.
May 14, 2014 at 5:08 PM
Quinisext wrote:
You can create alias using MyClass = RatherLongNameOfClass; and it'll work for everything but constructors'/destructors' names.
I know, but it would be handy to have it builtin by default in keywords like class/struct.

Quinisext wrote:
Unlikely worthy.
YMMV: I deem it really worthy, an not only for static members in the same class but also for static members in base classes.