Code compiling and running environment Visual Studio 2010. NET v4.0.30319
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace DelegateUseInCommonClass
{
/// <Summary>
/// General generic classes are designed to contain some standard task code, which contains the logic of how the task is executed, but sometimes these tasks contain some
/// Subtask. Only objects of the general class can know how to execute these subtasks. For example:
/// A general class for sorting objects is created. You need to know the specific sorting rules before sorting objects can be completed smoothly. For example, the int object is of a relatively large size,
/// The string object compares the first letter. Some complex objects may be sorted by comparing a specific attribute value.
/// In this case, the subtask can be executed by delegation, which facilitates users of the general class to execute the subtask.
/// </Summary>
Class Program
{
Static void Main (string [] args)
{
// Define and initialize three arrays of Different Types
Int [] intItems = {12, 2, 4, 5, 78, 32, 12, 65 };
String [] stringItems = {"test", "best", "active", "card", "book "};
Person [] personItems = {new Person (12, "Mark"), new Person (45, "Mary"), new Person (32, "Oscar "), new Person (80, "Kele"), new Person (12, "Ada ")};
# Sorting of region Int type
Console. WriteLine ("Before sorting intItems :");
Console. Write ("");
Foreach (int I in intItems)
{
Console. Write (string. Format ("{0,-4}", I ));
}
Console. WriteLine ();
// Sort intItems
CommonClass. Sort <int> (intItems, (item1, item2) =>
{
If (item1> item2)
Return true;
Else
Return false;
});
Console. WriteLine ("after intItems is sorted by Int value from small to large :");
Console. Write ("");
Foreach (int I in intItems)
{
Console. Write (string. Format ("{0,-4}", I ));
}
Console. WriteLine ();
# Endregion
# Sort by region String type
Console. WriteLine ("Before sorting stringItems :");
Console. Write ("");
Foreach (string str in stringItems)
{
Console. Write (string. Format ("{0,-10}", str ));
}
Console. WriteLine ();
// Sort stringItems
CommonClass. Sort <string> (stringItems, (item1, item2) =>
{
If (item1.CompareTo (item2)> 0)
Return true;
Else
Return false;
});
Console. WriteLine ("after stringItems is sorted by the first letter of string :");
Console. Write ("");
Foreach (string str in stringItems)
{
Console. Write (string. Format ("{0,-10}", str ));
}
Console. WriteLine ();
# Endregion
# Ranking of the region Person type
Console. WriteLine ("Before sorting personItems :");
Foreach (Person per in personItems)
{
Console. WriteLine (string. Format ("Person: Name-{0,-10} Age-{1}", per. Name, per. Age ));
}
CommonClass. Sort <Person> (personItems, (item1, item2) =>
{
If (item1.Age> item2.Age)
Return true;
Else
Return false;
});
Console. WriteLine ("after personItems are sorted by age from small to large :");
Foreach (Person per in personItems)
{
Console. WriteLine (string. Format ("Person: Name-{0,-10} Age-{1}", per. Name, per. Age ));
}
CommonClass. Sort <Person> (personItems, (item1, item2) =>
{
If (item1.Name. CompareTo (item2.Name)> 0)
Return true;
Else
Return false;
});
Console. WriteLine ("after personItems is sorted by the first letter of the name :");
Foreach (Person per in personItems)
{
Console. WriteLine (string. Format ("Person: Name-{0,-10} Age-{1}", per. Name, per. Age ));
}
# Endregion
}
// Define the Person class
Class Person
{
Private int _ Age;
Private string _ Name;
Public int Age
{
Get
{
Return _ Age;
}
Set
{
_ Age = value;
}
}
Public string Name
{
Get
{
Return _ Name;
}
Set
{
_ Name = value;
}
}
Public Person (int age, string name)
{
_ Age = age;
_ Name = name;
}
}
}
# Region Generic Design
/// <Summary>
/// Here, sort is used to describe the use of delegation in common classes. This method is described in multiple C # statements, including:
/// C # Essence
/// C # Advanced Programming
/// </Summary>
Class CommonClass
{
// Define the delegate type used to compare two objects and return the bool type
Public delegate bool Compare <T> (T item1, T item2 );
// General sorting method, which can be used to Compare any objects. The comparison result depends on compare of the subtask <T> type.
Public static void Sort <T> (T [] items, Compare <T> compare)
{
For (int I = 0; I <items. Count ()-1; I ++)
{
For (int j = I; j <items. Count (); j ++)
{
If (compare (items [I], items [j])
{
T temp = items [I];
Items [I] = items [j];
Items [j] = temp;
}
}
}
}
}
# Endregion
}
Running result: