简体   繁体   中英

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:

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? And what does actually gets passed as an argument.

Thanks

PS: I indeed forgot the static part! 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. 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 . (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 .

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. 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. This is why the error regarding an unrecognized method includes the no extension method accepting a first argument of type... clause.

This is a C# "Extension method" feature. It allows you to extend other class with your code.

So for example you can add an new method to "int" type.

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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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