简体   繁体   English

传递了什么样的论点?

[英]What kind of argument is passed?

I am relatively new to C# and I was wondering what kind of argument is passed in the Window partial class in the following function: 我是C#的新手,我想知道在以下函数的Window部分类中传递什么样的参数:

public static void process(this Window window){...}

I know the type is a Window , but why does it seem to have argument - type - argument syntax? 我知道类型是Window ,但是为什么它似乎具有参数-类型-参数语法? And what does actually gets passed as an argument. 实际传递的是什么作为参数。

Thanks 谢谢

PS: I indeed forgot the static part! PS:我确实忘记了静态部分! Sorry guys! 对不起大家!

Assuming you forgot the static part of the signature, this is an extension method. 假设您忘记了签名的静态部分,这是一种扩展方法。 An alternative to the decorator pattern to allow you to add functionality to existing classes. 装饰器模式的替代方法,使您可以向现有类添加功能。 this in the signature specifies that this is the class you want to extend. this在签名指定,这是要扩展的类。 for example: 例如:

var w = new Window();
w.Process();

In this method you don't pass any argument. 在这种方法中,您不会传递任何参数。 It is an extension method for the type called Window . 它是称为Window的类型的扩展方法。 (In order to not be misunderstood, we can pass arguments to an extension method, but this extension method hasn't any arguments.).So it can be used like below: (为了不被误解,我们可以将参数传递给扩展方法,但是此扩展方法没有任何参数。)因此可以如下使用:

window.process()

where window is an instance of Window . 其中windowWindow的实例。

For further documentation about extension methods, please have a look here . 有关扩展方法的更多文档,请在此处查看

assuming you meant to add a static modifier, this is an Extnsion Method, which can be caled via object - method syntax. 假设您打算添加static修饰符,则这是一个Extnsion方法,可以通过object-method语法进行校准。 Despite this, the compiler will turn it into type - method ( object ) syntax at compile time, it's just arranged the first way as syntactical sugar. 尽管如此,编译器仍会在编译时将其转换为type-method(object)语法,它只是第一种方式作为语法糖。 This is why the error regarding an unrecognized method includes the no extension method accepting a first argument of type... clause. 这就是为什么有关无法识别的方法的错误包括no extension method accepting a first argument of type...子句no extension method accepting a first argument of type...

This is a C# "Extension method" feature. 这是C#“扩展方法”功能。 It allows you to extend other class with your code. 它允许您使用代码扩展其他类。

So for example you can add an new method to "int" type. 因此,例如,您可以向“ int”类型添加新方法。

The method you posted is extending the Window class, adding a "process" method to it (it should be named like "Process", according to C# standards). 您发布的方法是扩展Window类,并向其添加“过程”方法(根据C#标准,它应命名为“过程”)。 It allows you to use in the code, like that: 它允许您在代码中使用,如下所示:

window.process();

You cane read more how to use them here eg: http://msdn.microsoft.com/en-us/library/bb383977.aspx 您可以在此处详细了解如何使用它们,例如: http : //msdn.microsoft.com/zh-cn/library/bb383977.aspx

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

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