C # generics (i)

Source: Internet
Author: User

Brief introduction:

Let's look at the concept of generics-"using parameterized types to manipulate multiple data types on the same code." Use the parameterized type to abstract the type for flexible reuse.

Many beginners will find it difficult to understand generics when they start to touch generics. It's easier to understand that "generics" is used as an adjective, because a lot of things can be generic, like- -

"Generic class", "generic Method", "Generic Interface", "generic delegate", etc... Most of the time we use generics to greatly reduce code duplication and to make the program more refreshing, and to avoid unnecessary ' boxing ' of the ' unboxing ' process.

Introduction to < generics | Why is there a generic type? >

In the process of programming we often encounter this situation: in order to achieve a certain function we start to write the method, but later we found that the same function needs us to write again but this method of the parameter type and the last different, this time according to the idea of agile software development, Do not prematurely abstract and respond to changes, when the change first occurs, use the quickest way to solve it, but the second time the change occurs, the better the architecture is designed so as to avoid over design, because it is possible that the second change will never occur. Given the functionality, we usually copy the code of the original method directly, and then modify the parameter type to resolve it quickly; it's true, but sometimes it's not just the second change that happens ... Or more changes, the continuation of the use of CV Dafa modification method signature will cause a lot of duplication of code, so we would like to think, if there is a way to pass any data type is good, that is, the implementation of this method as a template to abstract the signature of the method, so we introduced the generic type.

Let's take a look at a specific example:

1. General wording:

-Enter multiple int types to bubble sort so that they are in turn small to large output, the code is as follows:

public class Sorthelper    {public        void Bubblesort (int[] arr)        {            int length = arr. Length;            for (int i = 0, i < length-1; i++)            {for                (int j = 0; J < Length-1-i; J + +)                {                    if (arr[j]>arr[j+1])                    {                        int temp=arr[j];                        ARR[J] = arr[j + 1];                        Arr[j + 1] = temp;}}}}        

Test:

  The console program outputs    static void Main (string[] args)        {            Sorthelper sorter = new Sorthelper ();            Int[] A = {4,5,1,3,2,8,5,0,2};            Sorter. Bubblesort (a);        }

output is:0,1,2,2,3,4,5,5,8

---------------enter multiple byte types, bubble-sort them down to large output in turn, and the code is as follows:

This time, I just need to copy the original method and change the signature.

public class Sorthelper    {public        void Bubblesort (byte[] arr)        {            int length = arr. Length;            for (int i = 0, i < length-1; i++)            {for                (int j = 0; J < Length-1-i; J + +)                {                    if (arr[j]>arr[j+1]) c10/>{                        Byte temp = arr[j];                        ARR[J] = arr[j + 1];                        Arr[j + 1] = temp;}}}}    

This is possible, but in the future to deal with a variety of other data classes n a large number of repeated replication seriously affect the simplicity of the code, and when the function to expand, each method must be modified, maintenance is very inconvenient.

1.2 Using generics (generic classes):

It's natural for us to think about it. If you can use a "placeholder" for the parameter type in a method, you can use this method as a template (a bit like a placeholder in HTML for Web programming ) to indicate what type he is in.

Here we use "T" to be this special parameter type, so the code becomes this:

public class Sorthelper    {public        void Bubblesort (t[] arr)        {            int length = arr. Length;            for (int i = 0, i < length-1; i++)            {for                (int j = 0; J < Length-1-i; J + +)                {                    if (arr[j]>arr[j+1]) c10/>{                        T temp = arr[j];                        ARR[J] = arr[j + 1];                        Arr[j + 1] = temp;}}}}    

C # generics (i)

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.