Tuesday, 15 September 2015

c# - How to create a generic extension method? -


I want to develop a generic extension method which should be arranged in a longer chain sequence.

I mean

  string [] name = {"John", "Mark", "Joel", "Thomas", "Compass", "Konrad" "Andrew", "Brian", "Bill"}; Var query = names.OrderBy (a => a.Length). Since then (a = & gt; a); What is the way to develop a generic extension method?  

I tried:

  Public Static Class Extension Operations {Public Fixed T [] Alpha Lananda Vice & lt; T & gt; (This t [] name) {var query = names.OrderBy (a => a.Length). Since then (a = & gt; a); Return query; Error 1: Definition for T-Length does not include  

Error 2: Can not convert

Code> System.Linq.IOrdiredEnumerable to T [] .

The first error is because the length string < / Code> is the asset of the square, whereas the type of T parameter in your normal version is not known, it can be of any kind.

The second error is because you return only the query object, but not the actual results. You may need to call ToArray () before returning.

With little modifications you can come up with:

  Public Static Class Extension Operation {Public Fixed IEnumerable & lt; T & gt; Alpha lengthwise & lt; T, L & gt; (This IEnumerable & lt; T & gt; name, funk & lt; T, L & gt; length provider) {Name back Order By (a => Length Provider (A)). Then (a => a); }}  

You can use it:

  string [] name = {"John", "Mark", "Joel", "Thomas "," Copsy "," Conrad "," Andrew "," Brian "," Bill "}; Var Results = Name Alpha Long Weise (a => a.Length);  

1 comment:

  1. लिखने के कार्यक्रम के लिए csharp प्रोग्रामिंग उदाहरण कोड
    आयत नमूना कार्यक्रम के लिए ज्यामिति रूपांतरण

    ReplyDelete