为什么我们使用"本"在扩展方法? [英] Why we use "this" in Extension Methods?

查看:106
本文介绍了为什么我们使用"本"在扩展方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问,为什么我们在一个扩展方法的参数(C#语言)...........
这样的函数之前使用this关键字:

I want to ask why we use "this" keyword before the parameter in an extension method (C# Language)........... like this function :

    public static int ToInt(this string number)
    {
        return Int32.Parse(number);
    }



我知道,我们不得不使用它,但我不知道为什么。

I know that we have to use it but I don't know why.

推荐答案

由于这是你告诉它摆在首位的扩展方法编译器的方式。否则,它只是一个正常的静态方法。我猜他们选择了这个所以他们没有拿出新的关键字,并可能打破旧的代码。

Because that's the way you tell the compiler that it's an extension method in the first place. Otherwise it's just a normal static method. I guess they chose this so they didn't have to come up with a new keyword and potentially break old code.

这篇关于为什么我们使用"本"在扩展方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆