简体   繁体   English

在C#语言中使用的是什么?

[英]What is 'this' used for in C# language?

I've read open source c# code and there is a lot of strange grammar (to me). 我读过开源的c#代码,并且有很多奇怪的语法(对我而言)。

They declare method arguments with the this keyword like this: 他们使用this关键字声明方法参数,如下所示:

this object @object 这个对象@object

What does it mean? 这是什么意思?

If I remove 'this' keyword where is before the data type, then will it work differently? 如果我删除'this'关键字在数据类型之前的位置,那么它会以不同的方式工作吗?

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. @符号允许变量名称与C#关键字相同 - 我倾向于像个人瘟疫一样避免使用它们。

If you remove the this keyword, it will no longer be an extension method, just a static method. 如果删除this关键字,它将不再是扩展方法,只是静态方法。 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. 编译器将简单地将所有“扩展方法调用”转换为标准静态方法调用,但是扩展方法调用仍然只能根据this type name语法对有效的扩展方法起作用。

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. 扩展方法的可发现性可能是一个问题,因此请注意您选择包含它们的命名空间。我们在.NET命名空间(例如System.Collections或其他)下有非常有用的东西。 Less useful but otherwise "common" stuff tends to go under Extensions.<namespace of extended type> such that discoverability is at least consistent via convention. 不那么有用但其他“常见”的东西倾向于在Extensions.<namespace of extended type>Extensions.<namespace of extended type>使得可发现性至少通过约定是一致的。
  • Try not to extend often used types in broad scope, you don't want MyFabulousExtensionMethod appearing on object throughout your app. 尽量不要在广泛的范围内扩展经常使用的类型,您不希望MyFabulousExtensionMethod出现在整个应用程序的object 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. 如果需要 ,可以将范围(命名空间)限制为非常具体,或绕过扩展方法并直接使用静态类 - 这些不会污染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). 在扩展方法中,“this”可以为null (由于它们如何编译成静态方法调用)所以要小心并且不要假设“this”不为null(从调用端看起来这看起来像是一个成功的方法调用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. 'this type name'语法用于扩展方法。

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: 例如,如果我想将一个UnCamelCase方法添加到字符串(所以我可以做"HelloWorld".UnCamelCase()来生成“Hello World” - 我写这个:

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. this string text表示您正在使用的字符串的特定实例,text是其标识符。

The @ syntax allows for variable names that are ordinarily reserved. @语法允许通常保留的变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM