More effective tive C # item8: use generic methods whenever possible, unless you need to use the type parameter in the instance Field

Source: Internet
Author: User

We need to discuss the trade-off between generic classes and generic methods.

In general, we can easily be restricted by the definition of generic types, but in many cases, we can use the generic method. The generic method is recommended because when a generic class is used, the C # compiler must generate valid il for the entire generic class according to the given constraints, in addition, the constraints must satisfy the needs of the entire class, while generic methods only need to satisfy the constraints of generic methods, in this way, we can set different constraints for different methods in the same class. The Compiler matches different overload forms according to different constraints, and the user will feel clearer.

Let's look at the following code.

Code

  1 public static class UtilWithGenerics<T>
2 {
3 public static T Max(T left, T right)
4 {
5 return Comparer<T>.Default.Compare(left, right) < 0 ? right : left;
6 }
7 }
8
9  public static class UtilWithoutGenerics
10 {
11 public static T Max<T>(T left, T right)
12 {
13 return Comparer<T>.Default.Compare(left, right) < 0 ? right : left;
14 }
15
16 public static int Max(int left, int right)
17 {
18 return Math.Max(left, right);
19 }
20
21 public static double Max(double left, double right)
22 {
23 return Math.Max(left, right);
24 }
25 }

The code above provides two solutions for obtaining the maximum value. One is to use generic classes, and the other is to use generic methods.

The following code is a test program for the above Code.

Code

 1 private static void Test()
2 {
3 Console.WriteLine(UtilWithGenerics<int>.Max(1, 2));
4 Console.WriteLine(UtilWithGenerics<string>.Max("A", "B"));
5 Console.WriteLine(UtilWithoutGenerics.Max(1, 2));
6 Console.WriteLine(UtilWithoutGenerics.Max("A", "B"));
7 }

The execution result is very simple and will not be repeated here.

Let's take a look at the above Code. Pay attention to the following statements:

 1 Console.WriteLine(UtilWithGenerics<string>.Max("A", "B"));
2 Console.WriteLine(UtilWithoutGenerics.Max<string>("A", "B"));
3 Console.WriteLine(UtilWithoutGenerics.Max("A", "B"));

The execution results of the preceding three statements are the same, that is, "B" is output to the console.

But from the preceding statement, we can see the following two problems: 1) if you use the first or second sentence, you still need to execute the generic type, which is troublesome. On the contrary, the third sentence does not specify the type, but is determined by the editor. It is better in terms of ease of use. This advantage can only be used for generic methods and cannot be used for generic classes. 2) since the generic method can be used by the compiler to determine the overloaded mode during runtime, the generic method can provide better scalability. For example, we can append a static method in the utilwithourgenerics class: max (string left, string right). In this way, when the program is executed, the code in the third sentence will call the reload form of a non-generic version, this modification method is only implemented in the utilwithoutgenerics class and does not need to be modified. This is very important for post-project maintenance.

Although the generic method has many advantages, in some cases, we still need to use generic classes, such as: 1) the class itself needs to store the type parameter object as its internal State (such as set type); 2) the class implements a generic interface, which must be a generic class.

 

In summary, the use of generic methods has the following advantages over generic classes: 1) simplifying the call process; 2) improving the scalability and maintainability of the program.

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.