C # 2.0 new features to explore the simulation of generics and built-in algorithms

Source: Internet
Author: User
Algorithm in c#2.0, anonymous method, IEnumerable interface and anonymous method of cooperation, make a lot of programming tasks become very simple, and write the program is very beautiful.

For example, we could write the following code:

List thelib = Library.getbooks ();

List found = Thelib. FindAll (Delegate (book Curbook)
{
if (Curbook.isbn.StartsWith ("..."))
return true;
return false;
});

foreach (book B in found)
Console.WriteLine (B.ISBN);

This program is very simple to show us the information we need to find, and the code is very straightforward. The built-in data structure gives us powerful algorithm support, but is it possible to define a similar algorithm for a custom class?

For example, if I have a custom library class that does not use list <Book> to store data, but instead uses some sort of custom data structure, can I also allow users to use similar syntax, ignoring the use of anonymous delegates to store details to implement a particular algorithm?

The answer is certainly yes, and in C # It is very simple to implement such a function.

First, let's look at the prototype of the anonymous delegate used in FindAll.

Public delegate BOOL predicate <T> (T obj);

Obviously, the code above is equivalent to registering a callback for a search, and defining some sort of traversal mechanism within the list, thus achieving a beautiful algorithm structure closure.

Seeing these, we can define our own algorithm structure, first of all, I define a class like the following

public class Myvec
{
public static Myvec operator + (Myvec A, T b)
{
A._list. ADD (b);
return A;
}
public override string ToString ()
{
StringBuilder builder = new StringBuilder ();
foreach (T A in _list)
{
Builder. Append (A.tostring ());
Builder. Append (",");
}
string ret = Builder. Remove (builder. Length-1, 1). ToString ();
return ret;
}

Public myvec findall (predicate Act)
{
Myvec t2 = new Myvec ();
foreach (T i in _list)
{
if (Act (i))
T2._list. ADD (i);
}
return T2;
}

This is the inner object
Private List _list = new list ();
}

This class contains a list <T> structure, mainly to verify that our idea is feasible, in fact, any structure that can support a foreach traversal can be used as a built-in data storage object, and we'll give a more complex implementation in the example that follows.

Here's the code for testing this experimental class:

static void Main (string[] args)
{
Myvec a = new Myvec ();
A + + 12;
A + + 15;
A + + 32;
Myvec B = A.findall (delegate (int x)
{
if (x) return true; return false;
}
);

Console.WriteLine ("Vection original");
Console.WriteLine (A.tostring ());
Console.WriteLine ("Vection found");
Console.WriteLine (B.tostring ());
Console.ReadLine ();
}


Compile, execute, program output:

Vection Original

12,15,32

Vection found

32

Exactly the same as we expected. Obviously, the algorithm inside the list is basically the same as we expected.

predicate <T> is simply a delegate used to emulate the implementation of a system, and can in fact use any delegate of its own definition as a function body of the callback.

By using the Ienumberable interface, you can traverse any structure to define powerful algorithm support for any data structure.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.