为什么是“静态”?和“这个”扩展方法需要什么,它们的内存是如何分配的?
关于扩展方法的几个问题:
为什么扩展方法是静态的?
为什么需要在静态类中声明它们?
扩展方法的参数列表中的this关键字表示什么?由于它是一个静态类,“this”关键字在这种情况下如何工作?
这些类型的方法如何进行内存分配?
A few questions about Extension Methods:
Why are Extension Methods static?
Why do they need to be declared in a static class?
What does the this keyword indicate in the parameter list of extension method? As it is a static class how does the "this" keyword work in this context?
How does memory allocation happen for these type of methods?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
静态方法和非静态方法之间的唯一区别是非静态方法接收隐式参数 -
this
。扩展方法不会在声明该方法的对象上下文中调用,因此无法向它们传递this
引用,因此它们必须是静态的。您不能在扩展方法中使用关键字
this
,我希望这能回答您的第三个问题。参数列表中的this
关键字只是用来指示此方法扩展的类型。您对内存分配有什么疑问?扩展方法就像任何其他静态方法一样,只是调用语法不同。
The only difference between a static and a non-static method is that a non-static method receives an implicit parameter -
this
. Extension methods are not called in the context of the object on which the method is declared, therefore there is no way to pass them thethis
reference, therefore they must be static.You cannot use the keyword
this
inside an extension method, I hope this answers your third question. Thethis
keyword in the parameter list is there just to indicate which type this method extends.What is your question about memory allocation? An extension method is just like any other static method, only the call syntax is different.
因为你不能修改类来添加实例方法。这都是语法糖,让这些方法的调用变得更好,仅此而已。
因为我们并不是要创建该类的实例。这是一个设计决定。为什么要让事情变得复杂,比如让某个类的实例附加了扩展方法,而这些扩展方法甚至可能无法使用?
这是一个静态方法。
this
在此上下文中没有任何意义。签名中的this
只是表示正在声明扩展方法的一种方式。对这个一头雾水。它们都是静态类上的静态方法。它们像任何其他(静态)方法一样被“分配”,该类型只有一份代码副本。
Because you cannot modify the class to add instance methods. It's all syntactic sugar to make calls to these methods nicer, nothing more.
Because we're not meant to create instances of the class. It's a design decision. Why complicate things like having instances of some class that has extension methods attached to them that might not even be usable?
It's a static method.
this
has no meaning in this context. Thethis
in the signature is just a way to denote that an extension method is being declared.Confused about this one. They are all static methods on a static class. They are "allocated" like any other (static) method, there's only one copy of the code for the type.
因为没有实例可以调用它们。
我认为它是在非静态类中声明时限制可能的错误数量。
它说的是它的扩展方法。编译器看到它并允许在声明为 this 的类型上使用它,而不是作为静态方法。
扩展方法调用只是语法糖。此调用被转换为 IL 中的普通静态方法调用。
Because there is no instance to call them on.
I think its to limit number of possible errors if it was declared in non-static class.
It says its extension method. Compiler sees this and allows to use it on type declared as this, instead as static method.
Extension method call is only syntactic sugar. This call gets converted into ordinary static method call in IL.
1,2,4的答案几乎是一样的。扩展方法只是带有一些特殊语法糖的静态方法。这
实际上与 IL 相同:
前一种方法使得提供 IntelliSense 支持变得更加容易。类需要静态的限制可能只是一个设计决策,或者可能与性能相关。
#3 的答案是,这只是语言设计者选择的语法。 C# 开发人员习惯使用
this
引用实例对象。他们可以将其称为“blah”,但对于实例对象是什么来说,它不会那么明显。在扩展方法中使用 this 关键字还会告诉 C# 编译器使用 扩展属性。程序集的使用者使用此属性来查找扩展方法。
The answer to 1,2, and 4 is pretty much the same. Extension methods are simply static methods with some special syntactical sugar. This:
Is effectively the same IL as:
The former method makes it much easier to provide IntelliSense support though. The restriction of the class needing to be static is probably just a design decision, or maybe performance related.
The answer to #3 is that's just the syntax the language designers chose. C# developers are used to
this
refering to the instance object. They could have called itblah
, but it wouldn't have been as obvious as to what the instance object is.Using the this keyword in an extension method also tells the C# compiler to mark it with the ExtensionAttribute. This attribute is used by consumers of the assembly to find extension methods.
这就是它们的定义方式 - this 关键字位于第一个参数之前,该参数是您调用方法的对象的实例。为什么 ToString() 称为 ToString()?为什么要用static来静态呢?草为什么是绿的?我猜想决定将 this 放在第一个参数前面的权力,而不是提出一个新的关键字“扩展”放在方法名称前面。
编译器生成的 IL 会将代码转换为对静态方法的调用,即使语法可能看起来像对实例方法的调用。但事实并非如此——它是静态的,它无法访问对象的私有成员。
That's just how they're defined - with this keyword preceding the first parameter, which is the instance of the object you're calling the method on. Why is ToString() called ToString()? Why is static used for static? Why is the grass green? I guess the powers that be decided to put a this in front of the first parameter, instead of coming up with a new keyword "extension" to put in front of method name.
IL-generated by the compiler translates your code into a call on the static method, even though the syntax may look like a call on an instance method. But it's not - it's a static, which has no access to your object's private members.