Is there a 'this' equivalent in C # for static members?

Is there an equivalent of this in C # for static members?

I like to use this to make my code more readable, but wondered if there was an equivalent for static members.

+6
c #
source share
5 answers

I suggest that if you use this. To enhance what you mean by instance members, the equivalent in a static member should be to use ClassName.

But stylistically, why add code that doesn't change the meaning?


edit to add various explanations:

My last sentence above can be illustrated by the following examples:

 class Example1 { public int J { get; set; } public Example1() { J = 0; } // These two methods have *exactly* the same CIL public int InstanceMethodLong() { return this.J; } public int InstanceMethodShort() { return J; } } 

this. in InstanceMethodLong does not change the value compared to InstanceMethodShort .

Statically:

 class Example2 { public static int K { get; set; } static Example2() { K = 0; } // These two methods have *exactly* the same CIL public int StaticMethodLong() { return Example2.K; } public int StaticMethodShort() { return K; } 

Example2. in StaticMethodLong does not change the value compared to StaticMethodShort .

In both cases, adding a qualifier leads to the same CIL, to the same behavior, and is more a source for writing, reading, and understanding. Stylistically - and I gladly agree that this is a code style issue - I see no reason for it to be there.


With underscores, the situation is slightly different:

 class Example3 { int _j; public int J { get { return _j; } set { _j = value; // and do something else, // to justify not using an auto-property } } public Example3() { J = 0; } public int MethodWithParameter(int j) { // Now there is a *difference* between return j; // and return _j; } } 

Here, in MethodWithParameter , there is a difference between the _j and j reference - so we intentionally and explicitly express different meanings. It’s true that the compiler doesn’t care what we call the names of our variables, but it doesn’t matter what variables we mean! Thus, in the body of MethodWithParameter using or not using underscores is not just stylistic, but also semantic. This is not the problem we are addressing in this matter.

+13
source share

Since the static member should not belong to any particular instance (since this refers to an instance of an object with different settings possible for each instance), instead you want to use ClassName.Member instead of this.Member .

 public class Orange { public static string Tastes = "sweet"; public static string FoodType(){ return "fruit"; } } 

Call:

 Console.WriteLine(Orange.Tastes); 

The same applies to static methods:

 Console.WriteLine(Orange.FoodType()). 

Please note that this is a contrived example for demonstration only. :)

+3
source share

You can use the class name to refer to other static properties.

Your code becomes a little more copy / paste resistant, but this is not always bad.

+2
source share

Unfortunately no, for static methods there is no this . To help differentiate static members from class members, I prefix it with the class name.

 class Test { static Regex TextRegex = new Regex(...); public static bool TestString(string input) { return Test.TextRegex.IsMatch(input); } } 
+2
source share

I like "this", as well as the implementation at a glance, where the state changes. In this case, you can consider the type name for static members.

+1
source share

All Articles