Generics with constraint

  

Scenario:

Per MSDN: Generics introduce the concept of type parameters to .NET, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code. For example, by using a generic type parameter T, you can write a single class that other client code can use without incurring the cost or risk of runtime casts or boxing operation.

Solution:

         User class
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    public class User
        {
            public User(int id, string name)
            {
                ID = id;
                Name = name;
            }
            public int ID { get; set; }
            public string Name { get; set; }
        }
          Generic List, finding records by name

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    public class GenricList<T> where T: User
        {
            private class Node
            {
                public Node(T t)
                {
                    next = null;
                    data = t;
                }
    
                private Node next;
    
                public Node Next
                {
                    get { return next; }
                    set { next = value; }
                }
    
                private T data;
    
                public T Data {
                    get { return data; }
                    set { data = value; }
                }
            }
    
            private Node head;
    
            public GenricList()
            {
                head = null;
            }
    
            public void AddHead(T t)
            {
                Node n = new Node(t);
                n.Next = head;
                head = n;
            }
    
            public IEnumerator<T> GetEnumerator()
            {
                Node current = head;
    
                while(current != null)
                {
                    yield return current.Data;
                    current = current.Next;
                }
            }
    
            public T Find(string name)
            {
                Node current = head;
    
                T t = null;
    
                while(current != null)
                {
                    if (current.Data.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        t = current.Data;
                        break;
                    }
                    else
                    {
                        current = current.Next;
                    }
                }
    
                return t;
            }
        }

         Calling GenericList
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public class GenericsClasswithConstraint
        {
            static void Main(string[] args)
            {
                GenricList<User> list = new GenricList<User>();
                for (int i = 0; i < 5; i++)
                {
                    list.AddHead(new User(1, "Ram"));
                    list.AddHead(new User(1, "Shyam"));
                    list.AddHead(new User(1, "Seeta"));
                    list.AddHead(new User(1, "Geeta"));
                }
    
                var user = list.Find("Ram");
    
                Console.WriteLine($"User {user.Name} has ID: {user.ID} ");
    
                Console.ReadLine();
            }
        }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...