Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using revision;
- using System.Collections;
- using System.Numerics;
- public class Program
- {
- static void Main(String[] args)
- {
- // var genList = new GenericList<Person>();
- // genList.add(new Person(1 , "Abeer" , "012" , "Damietta" ));
- var genList = new List<string>();
- genList.Add("Abeer");
- genList.Add("Sara");
- genList.Add("Malak");
- foreach (var item in genList)
- {
- Console.WriteLine(item);
- }
- Console.WriteLine(Add(3 , 2));
- Console.WriteLine(Add(3.4 , 5));
- // Console.WriteLine(Add(3 , "string")); xxx
- }
- static T Add<T>(T num1, T num2) where T : INumber<T>
- {
- return num1 + num2;
- }
- public class GenericList<T> :IEnumerable<T> where T : class /* class => means that T should be a reference type */ , new() /* with paramterless constructor*/
- {
- private readonly List<T> list = new List<T>();
- public void remove(T item)
- {
- list.Remove(item);
- }
- public void add(T item)
- {
- list.Add(item);
- }
- public IEnumerator<T> GetEnumerator()
- {
- foreach (T item in list)
- {
- yield return item; // Yield each item in the collection
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return GetEnumerator();
- }
- public int counter => list.Count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement