Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'this' used for in C# language?

Tags:

syntax

c#

.net

this

I've read open source c# code and there is a lot of strange grammar (to me).

They declare method arguments with the this keyword like this:

this object @object

What does it mean?

If I remove 'this' keyword where is before the data type, then will it work differently?

like image 541
Sung Am YANG Avatar asked May 10 '13 12:05

Sung Am YANG


People also ask

Why #is used in C?

In C/C++, the # sign marks preprocessor directives. If you're not familiar with the preprocessor, it works as part of the compilation process, handling includes, macros, and more. It actually adds code to the source file before the final compilation.

What does %= mean in C?

%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A.


2 Answers

Sounds like an Extension Method.

The @ symbol allows the variable name to be the same as a C# keyword - I tend to avoid them like the plague personally.

If you remove the this keyword, it will no longer be an extension method, just a static method. Depending on the calling code syntax, it may no longer compile, for example:

public static class IntegerMethods
{
    public static int Add(this int i, int value)
    {
        return i + value;
    }
}

int i = 0;

// This is an "extension method" call, and will only compile against extension methods.
i = i.Add(2); 
// This is a standard static method call.
i = IntegerMethods.Add(i, 2);

The compiler will simply translate all "extension method calls" into standard static method calls at any rate, but extension method calls will still only work against valid extension methods as per the this type name syntax.

Some guidelines

These are my own, but I find they are useful.

  • Discoverability of extension methods can be a problem, so be mindful of the namespace you choose to contain them in. We have very useful stuff under .NET namespaces such as System.Collections or whatever. Less useful but otherwise "common" stuff tends to go under Extensions.<namespace of extended type> such that discoverability is at least consistent via convention.
  • Try not to extend often used types in broad scope, you don't want MyFabulousExtensionMethod appearing on object throughout your app. If you need to, either constrain the scope (namespace) to be very specific, or bypass extension methods and use a static class directly - these won't pollute the type metadata in IntelliSense.
  • In extension methods, "this" can be null (due to how they compile into static method calls) so be careful and don't assume that "this" is not null (from the calling side this looks like a successful method call on a null target).

These are optional and not exhaustive, but I find they usually fall under the banner of "good" advice. YMMV.

like image 168
Adam Houldsworth Avatar answered Oct 25 '22 12:10

Adam Houldsworth


The 'this type name' syntax is used for extension methods.

For example if I wanted to add a UnCamelCase method to a string (so I could do "HelloWorld".UnCamelCase() to produce "Hello World` - I'd write this:

public static string UnCamelCase(this string text)
{
    /*match any instances of a lower case character followed by an upper case 
     * one, and replace them with the same characters with a space between them*/
    return Regex.Replace(text, "([a-z])([A-Z])", "$1 $2");
}

this string text means the specific instance of the string that you're working with, and text is the identifier for it.

The @ syntax allows for variable names that are ordinarily reserved.

like image 23
PhonicUK Avatar answered Oct 25 '22 12:10

PhonicUK