Generic is referenced in. net2.0. Generic is one of my favorite features in. net 2.0. It can be said that generics have brought a great impact to our programming ideology .. Compared with. net1.1, net2.0 has many functions that can be implemented by both generic and non-generic functions, such as filtering an array element. However, the use of generics brings many additional benefits, or generics can do many tasks that cannot be completed in the non-generic era.
Case 1:Object-oriented packaging is used for adding, deleting, modifying, and querying. If you want to better decouple the client from business processing and data processing, it is very difficult to implement it in the. net1.1 era. For example, many open-source frameworks have powerful ORM programming modes, which enables all addition, deletion, modification, and query operations to be uniformly represented in interface methods, the accepted parameter is an object and does not depend on the specific data source type.
Case 2:IComparer <T> API extension, IComparer <T> API provides a flexible solution for sorting object sets. Compared with the IComparer API, it eliminates annoying type conversion and type security issues. We can further encapsulate the IComparer <T> interface to make it more decoupled on the client. This article can be used as a parameter: generics are amazing.
Case 3:For an array or object set query, the direct point is to implement IEnumerable, IEnumerable <T> or a derived type, such as the IQueryable <T> interface set. The traditional IEnumerable interface requires the foreach operation when querying data, while the generic IEnumerable interface or derived type, such as IQueryable <T> interface, it provides a simpler and more efficient and secure query mode.
Example:The following is a simple example to illustrate the generic applications in LINQ.
// Declare an integer Array
Int [] num = {0, 1, 2, 3, 4, 5, 6, 7, 8 };
// Query the set with element values greater than 1 in the array
// Traditional Method
ArrayList listArr = new ArrayList ();
Foreach (int I in num)
{
If (I <1)
{
ListArr. Add (I );
}
}
// Use the LINQ Method
// Anonymous type
Var list = from _ num in num
Where _ num> 1
Select _ num;
Disadvantages of non-generic mode:
1): From the Perspective of the amount of code, there are more than the LINQ method;
2): ArrayList is inherently inferior to the generic type in terms of performance.
3): Make full use of the advantages of generics and use the Excellent query methods of LINQ to further enhance code readability.
The following are some generic usage in LINQ that I have summarized in combination with MSDN. I forgot to correct the shortcomings.
1. IEnumerable <T> variable in the LINQ query.In the following code, IEnumerable <int> is displayed, indicating that only 0 or n integer data are stored in the queried group.
// Display type
IEnumerable <int> list2 = from _ num in num
Where _ num> 1
Select _ num;
Second, the system deduced the variable type.Since. net3.0 introduces the anonymous type, we can directly write IEnumerable <int> into var to let the system judge the type of the variable.
// Anonymous type
Var list = from _ num in num
Where _ num> 1
Select _ num;
Third: Query type relationships in operationsIn the second article, we know that the anonymous type can be referenced to let the system judge the type of the variable, but to write a valid query, we 'd better understand the relationship between objects. The data source, query itself, and query execution are strongly-typed. The type of the variable in the query must be compatible with the element type in the data source and the type of the iteration variable in the foreach statement.
1): Data sources do not need to be converted during the query operation. The element type of the query result set is the same as that of the data source element.In the first display type query, the record set element type is IEnumerable <int>. If we want to perform the foreach operation on the set, we need to write as follows:
Foreach (int I in list2)
{
// Dosomething
}
Foreach (var I in list2)
{
// Dosomething
}
2): Data type conversion is required in the query operation. If the source and the last queried data sources have different element types, type conversion is required.The following is a collection of students, but we only need to return the phone numbers of qualified students and do not need any other information. For the validity of the query, all information of qualified students is not required, that is, you do not need to return the entire student, which is equivalent to select * in SQL *. The following is a student class:
/// <Summary>
/// Student Entity
/// </Summary>
Class student
{
/// <Summary>
/// Last name
/// </Summary>
Public string sFirst
{
Get;
Set;
}
/// <Summary>
/// Name
/// </Summary>
Public string sLast
{
Get;
Set;
}
/// <Summary>
/// Phone number
/// </Summary>
Public string sPhone
{
Get;
Set;
}
}
Construct student data sources
// Student name and phone set
IEnumerable <student> listStudent = new List <student> {
New student {sFirst = "Zhang", sLast = "3", sPhone = "1111111 "},
New student {sFirst = "Li", sLast = "4", sPhone = "2222222 "},
New student {sFirst = "Li", sLast = "Ming", sPhone = "3333333 "}
};
Query the phone number of a student surnamed Li.
IEnumerable <string> listStudentQuery = from item in listStudent
Where item. sFirst = "Li"
Select item. sPhone;
Guess: if the original data source implements IEnumerable or IEnumerable <T>, the query result set also implements the data source interface.No relevant information is found here, which can be easily provided by some friends. Therefore, if the data source type is IEnumerable <student>, the obtained query result will also implement the IEnumerable <T> interface.
Error query:
IQueryable <string> listStudentQuery2 = from item in listStudent
Where item. sFirst = "Li"
Select item. sPhone;
3): Allows the system to deduce data types completely on behalf of the system. That is, the specified data type is not displayed during the query.Query the phone number of a student surnamed Li
Var listStudentQuery2 = from item in listStudent
Where item. sFirst = "Li"
Select item. sPhone;
Foreach (var s in listStudentQuery2)
{
// Dosomething
}
NOTE: Refer to MSDN for this article