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?
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.
%= 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.
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.
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.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.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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With