Generics-starting with simplicity (2)

Source: Internet
Author: User

Generics are classes, structures, interfaces, and methods with placeholders (type parameters, these Placeholders are one or more types of placeholders stored or used by classes, structures, interfaces, and methods. This definition is not a bit dizzy after reading it. Below we will illustrate this definition using simple languages and simple examples. (This is also the ultimate goal of my writing this series. I will understand important concepts from simple examples .) The following explains why generic is used. Generic is not used before. Net framework2.0, and there must be a reason for the emergence of anything. Generic classes and methods can postpone one or more types until the client Code declares and instantiates the class or method. This may be the biggest feature of generics. Some people may say that using the object class can also achieve this. How has it become a wildcard feature? It is true that objects are used before there is no generics, but the usage of generics must be extraordinary. There is no packing or unpacking burden for the generic type, which reduces the errors caused by type conversion, and allows code to be reused to the maximum extent to protect the type security and improve performance. Simply protecting the security of the type may attract many programmers. Let's take a look at the following small example. First, I want to explain to friends who are new to the generic type how to use the generic type, the second is to explain why protection type security can be liked, and the third is to explain what other benefits are there to use generics.

 

Use object
1 public class stackobj
2 {
3 object [] items = new object [100]; // defines an array of the Object Type
4 int COUNT = 0;
5 Public void push (Object item) // Add an item to the array
6 {
7 items [count] = item;
8 count ++;
9}
10 public object POP () // retrieve an item from the array
11 {
12 return items [count-1];
13}
14
15}
16 use generic
1 public class stackgen <t>
2 {
3 T [] items = new T [100];
4 int COUNT = 0;
5 Public void push (T item)
6 {
7 items [count] = item;
8 count ++;
9}
10
11 Public t POP ()
12 {
13 return items [count-1];
14}
15}

 

Comparing the above two sections of code, we can easily see that generics only use t to replace the object (actually not, but we can understand it first ).

Call
1 class Program
2 {
3 static void main (string [] ARGs)
4 {
5 int;
6 string B;
7 stackobj mystackobj = new stackobj ();
8 mystackobj. Push (10); // Add an integer Item 10 to the array.
9 A = (INT) mystackobj. Pop (); // because the array is an integer, use int to convert it.
10 // B = (string) mystackobj. Pop (); // No syntax error. The compilation is successful, but the running fails.
11
12 console. writeline ("shiyong object quchu de yige Xiang Shi {0}", );
13 console. writeline ();
14
15 stackgen <int> mystackgen = new stackgen <int> ();
16 mystackgen. Push (10 );
17 int c = mystackgen. Pop ();
18 // string d = mystackgen. Pop (); // compilation fails, and INT type cannot be implicitly converted to string
19 console. writeline ("shiyong fanxing quchu de yige Xiang Shi {0}", C );
20
21
22 console. readkey ();
23
24}

 

After the code is pasted, nothing can be said. Just adding that

Code
// String d = mystackgen. Pop (); // compilation fails, and INT type cannot be implicitly converted to string

When the generic type is used, this statement cannot be passed during compilation, so that program errors can be easily discovered.

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.