Generics Interface

  

Scenario:

Generics Interface

Solution:

     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
        public interface IData<T>: IDisposable
        {
            void Add(T data);
    
            T Get();
        }
    
        public class User<T>: IData<T>
        {
            T user;
            public void Add(T data)
            {
                this.user = data;
            }
    
            public void Dispose()
            {
                throw new NotImplementedException();
            }
    
            public T Get()
            {
                return this.user;
            }
        }
         Adding User
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    public class GenericsClasswithConstraint
        {
            static void Main(string[] args)
            {
                var user = new User<string>();
                user.Add("Ram");
    
                Console.WriteLine($"User \"{user.Get()}\" has been added");
    
                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...