SoFunction
Updated on 2025-03-08

A brief discussion on the application of c# generics


class GenericInterface
    {
        static void Main()
        {
            SortedList<Person> list = new SortedList<Person>();
            string[] names = new string[]
            {
                "zhang san",
                "li si",
                "wang wu",
                "zhou er",
                "he yi"
            };
            int[] ages = new int[] { 22, 15, 30, 34, 12 };
            for (int x = 0; x < 5; x++)
            {
                (new Person(names[x], ages[x]));
            }
            foreach (Person p in list)
            {
                (());
            }
("---------------------------------------------------------------------------------------------------------------------------------------------------------------);
            ();
            foreach (Person p in list)
            {
                (());
            }
            ();
        }
    }
    public class GenericList<T> : <T>
    {
        public class Node
        {
            private T data;
            public T Data
            {
                get { return data; }
                set { data = value; }
            }
            private Node next;
            public Node Next
            {
                get { return next; }
                set { next = value; }
            }
            private Node last;
            public Node Last
            {
                get { return last; }
                set { last = value; }
            }
            public Node(T t)
            {
                data = t;
                next = null;
            }
        }
        public Node firstNode;
        private Node lastNode;
        public void AddNode(T t)
        {
            Node node = new Node(t);
            = lastNode;
            if (lastNode != null)
                = node;
            lastNode = node;
            if (firstNode == null)
                firstNode = node;
        }
#region IEnumerable<T> Member
        public IEnumerator<T> GetEnumerator()
        {
            Node current = firstNode;
            while (current != null)
            {
//yield return expression is returned as an enumeration object
                yield return ;
                current = ;
            }
        }
        #endregion
#region IEnumerable Member
//IEnumerable < T > inherits from IEnumerable,
//So this type of GetEnumerator must implement generic and non-generic versions.
//In most cases, non-generic methods simply call generic methods.
        IEnumerator ()
        {
            return GetEnumerator();
        }
        #endregion
    }
    public class SortedList<T> : GenericList<T> where T : <T>
    {
//This method implements sorting
        public void BublleSort()
        {
            if (firstNode == null || == null)
                return;
            bool swapped;
            do
            {
                Node last = null;
                Node current = firstNode;
                swapped = false;
                while ( != null)
                {
                    if (() > 0)
                    {
/* The current node is larger than the next node, location exchange*/
                        Node tmp = ;
                        = ;
                        = current;
                        if (last == null)
                        {
                            firstNode = tmp;
                        }
                        else
                        {
                            = tmp;
                        }
                        last = tmp;
                        swapped = true;
                    }
                    else
                    {
                        last = current;
                        current = ;
                    }
                }
            }
            while (swapped);
        }
    }
    public class Person : <Person>
    {
        string name;
        int age;
        public Person(string n, int a)
        {
            name = n;
            age = a;
        }
#region IComparable<Person> Member
        public int CompareTo(Person p)
        {
//Sorted by age
            //return age - ;
//Sort by name
            int a =();
            return a;
        }
        #endregion
        public override string ToString()
        {
            return name + ":" + age;
        }
    }