扩展方法 [英] Extended Methods

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

问题描述

扩展方法=静态方法返回强类型变量或集合吗?

我读到一个有关它的条目,说-"this"关键字作为静态方法参数告诉编译器该特定扩展方法应为

那么我们是否需要添加"this"关键字来创建扩展方法,还是可以将静态类中的每个静态方法称为扩展方法?

Is extension method = static method returning strongly typed variables or collections?

i read one entry about it saying -- "this" keyword as a static method param tells The compiler that this particular Extension Method should be added to objects of type default .net class.

SO do we need to add "this" keyword for creating extended method or every static method in static class can be called extension method?

推荐答案

来自此处 [扩展方法是一种特殊的方法静态方法,但它们被称为扩展类型上的实例方法."

From here[^],
"Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type."

成员3981366写道:
Member 3981366 wrote:

返回强类型变量或集合


对不起,我不明白您的意思.


Sorry, I don''t understand what you mean.

成员3981366写道:
Member 3981366 wrote:

我们是否需要添加"this"关键字来创建扩展方法

do we need to add "this" keyword for creating extended method


是的,如果您希望它成为用作扩展方法,即编译器自动粘贴将静态扩展方法链接到实例对象的代码.


Yes, if you want it to be used as an Extension method i.e. the compiler automatically glueing the code for linking the static extension method to the instance object

成员3981366写道:
Member 3981366 wrote:

静态类中的每个静态方法都可以称为扩展方法

every static method in static class can be called extension method


否,因为您不能将静态方法与实例对象一起使用.扩展方法的主要用途.


No, since you cannnot use static methods with instance object. There lies the major inclination of extension methods.


是的.

当您在静态类中使用静态方法并使用"this"关键字时,此方法称为扩展方法.
Yes.

When you use static methods within static class and use "this" keyword then this method known as Extended methods.


到 指定扩展方法,您可以在
之前插入关键字this 第一个方法参数,向编译器指示您的类型
想扩展.请注意,该方法及其类必须是静态的.
To specify an extension method you insert the keyword this before the
first method parameter, which indicates to the compiler the type you
want to extend. Note that the method and its class must be static.


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

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