c# - "this" in function parameter -


I remember building any type of this type Is not it - can someone tell "this"? I thought that by proclaiming something public, it means that there is no need to make the classroom immediately - so what is "this" in this case?

This syntax is to declare the extension methods, a new feature of C # 3.0.

An extension method is part code, part compiler is "magic", where the intellisense in the compiler visual studio shows that your extension method is actually available as an example method on the object in question is.

Let me give an example.

The string class, named Gobble Gobble, lets create an extension method:

  public static class string extension {public static zero} GABBLE GOBBLE Strings) {Console.Out.WriteLine ("Gobble Gobble," + S); }}  

The name of the class is just my naming convention, it is not necessary to name it in such a way, but as a method it must be stable.

After declaring the above method, you can type it in Visual Studio,

  string s = "Turkish buster!"; S  

After the dot, wait for Intelligence, and notice that there is a GabbalGobble method, complete the code like this:

  String = "Turkish Buster!"; S.GobbleGobble ();  Important    Important     Important  If you type in GobbleGobble manually, and < Kbd> Ctrl  + .  Use shortcut, it will not help you get right when using the instructions in the file.  

Note that the parameters of the method have disappeared. The compiler will quietly move around important bits, which are:

  string s = "Turkish buster!"; S.GobbleGobble (); ^ ^ | + - In the compiler string extension class it will be found + - the first parameter of the method will be used  

Thus, the above code will be replaced by the compiler:

< Pre> string = "Turkish buster!"; StringExtensions.GobbleGobble (s);

So in call-time, there is nothing magical about it, it's just a call to a steady method.

Note that if your extension method declares more than one parameter, only the first supports it modifier, and the rest will be as normal as part of the call method. To be specified with:

  public static zero GobbleGobble (this string value, string extra) {| | ... | | } | | | | + -------------------------------------------- + | | | Wei | S.GobbleGobble ("The extra goes here"); | ^ | | | + ----------------------------------- +  

Extension Methods Link was added to the part, where the Linux syntax in C # will appear for the proper nomination methods for the object, which means that you can only "introduce" in any type of class by declaring the correct extension methods Are there. Of course, full Linq support is a lot of work, but it is possible.

In addition, self-expansion methods are really useful, so read on this.

  • Html>

    Comments

    Popular posts from this blog

    windows - Heroku throws SQLITE3 Read only exception -

    lex - Building a lexical Analyzer in Java -

    python - rename keys in a dictionary -