• Old Collections & Non Generic Collections →

    • ArrayList →

      • Array alternative
      class program 
      {
          public static int SumArrayList(ArrayList Lst)
          {
              int Sum = 0;
      
              for (int i = 0; i < Lst?.Count; i++)
                  Sum += (int)Lst[i];///UnBoxing , UnSafe
      
              return Sum;
          }
          
          static void Main(string[] args)
      	  {
              ArrayList aLst = new ArrayList();
      
              aLst.Add(1); ///Boxing
              aLst.Add("2");///Compiler can't Enforce Type Safety @ Compilation
              aLst.Add(3);
              aLst.AddRange(new int[] { 4, 5, 6, 7 });
      
              aLst.Remove(3);
      
              SumArrayList(aLst);
          }
      }
      

    Array ⇒ O(1)


  • Generic Collections →
    • List →

      • لو محتطش كابستي بيعمل ارري فاضي طول ما الليست فاضية اول ما اضيف ايليميت يعمل اوبجكت ارري جديد بكابستي 4 يخزن فيها عناصر الليست ولو عناصر الليست زادت عن 4 بيعمل ارري جديد بضعف كابستي القديم وينسخ الداتا من الارري القديم للجديد
      • Array is being allocated when its full
      class program 
      {
          public static int SumList ( List<int> Lst)
          {
              int Sum = 0;
      
              for (int i = 0; i < Lst?.Count; i++)
                  Sum += Lst[i];
      
              return Sum;
          }
      		
      		public void Main(string[] args)
      		{
      		    List<int> iLst = new List<int>();
      	
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      	
      	      iLst.Add(1);
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      	
      	      //iLst.Add("2"); //Compiler Enforce Type Safety At Compilation Type
      	      iLst.Add(2);
      	      iLst.Add(3);
      	
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      	
      	      ///iLst[5] = 20;   ///Argumentoutofrange Exception 
      	      ///use indexer for update and Get 
      	
      	      iLst.Add(4);
      	      iLst.Add(5);
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      	
      	      iLst.AddRange(new int[] { 6, 7, 8 });
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      	
      	      iLst.Add(9);
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      	
      	      iLst.TrimExcess();
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      	
      	      iLst.Add(10);
      	      Console.WriteLine($"Size {iLst.Count} , Capacity {iLst.Capacity}");
      		}
      }
      
    • Dictionary →

      class program 
      {
      		public void Main(string[] args)
      		{
      		    Dictionary<string, long> PhoneBook = new Dictionary<string, long>();
      
              PhoneBook.Add("ABC", 123);
              PhoneBook.Add("XYZ", 456);
              PhoneBook.Add("KLM", 789);
      
              //PhoneBook.Add("XYZ", 654); ///No Duplicate Keys allowed
      
              //PhoneBook["XYZ"] = 654; ///Update
      
              //if ( !PhoneBook.TryAdd("XYZ" , 654))
              //    PhoneBook["XYZ"] = 654; ///Update
      
              if (!PhoneBook.ContainsKey("XYZ"))
                  PhoneBook.Add("XYZ", 654);
              else
                  PhoneBook["XYZ"] = 654; ///Update
      
              PhoneBook["DEF"] = 707; /// Add
      
              Console.WriteLine(PhoneBook["XYZ"]); /// get
      
              //Console.WriteLine(PhoneBook["AXY"]); ///exception key not found
              if (PhoneBook.TryGetValue("AXY", out long V))
                  Console.WriteLine(V);
              else
                  Console.WriteLine("NA");
      
              foreach (KeyValuePair<string, long> item in PhoneBook)
              {
                  Console.WriteLine($"{item.Key}:::{item.Value}");
              }
      		}
      }
      
    • Queue →

    • Stack →

    • Set →


  • Note →

    • we can have two classes with the same name in one namespace (project) but if we have a generic class with the same name of existing class its ok
        class MyType { }
        class MyType<T> { }
        class MyType<T1 , T2> { }
    

  • C Pointer Style →

    1. Pointer Declaration
    2. Pointer Initialization
    3. using Pointer Access Data
    ///C Pointer Style
    int X = 5;
    ///1. Pointer Declaration
    int *Ptr;
    ///2. Pointer Initialization
    Ptr = &X;
    ///3. using Pointer Access Data
    Ptr = 8;
    

  • C# Pointer Style (Delegate) (Pointer to Function) →

    1. Delegate DataType Declaration
    2. Delegate object Declaration
    3. Pointer To Function ( Delegate Object ) Initialization
    4. using Pointer to Function , Call(invoke) Function

    • Pointer to Function →
      • شايل ادريس بيشاور ع فنكشن
      • in c & c++ -> was not type safe
      • in C# it has 2 usages →
        1. pass small func to big func
        2. call back method

    • Single delegate in Base Class Library (BCL) →
      • has 4 attribute →
        • _target →
          • = null when function is static
          • = this(object) when function is object function
        • _methodPtr , _methodPtrAux → point to the function address
        • _methodBase
      • IntPtr -> struct datatype carry pointer

    • Difference between interface and delegate →
      • can’t call static methods by using interface
      • before C# 8 -> interface cant point on non public method
      • interface cant point to method that its name is variable

    • Strategy and Template do same thing as delegate but they are design patterns and delegate is language feature

    • Delegate Ex01 →

      ///new Class (new Delegate) , Specify Function Signature
      public delegate int StringFuncDelDT(string S);
      
      class Program
      {
          static void Main(string[] args)
      	  {
              ///1. Delegate object Declaration
              StringFuncDelDT fPtr;
      
              ///2. Pointer To Function ( Delegate Object ) Initialization
              fPtr = new StringFuncDelDT(StringFunctions.GetLenght);
      
              ///3. using Pointer to Function , Call(invoke) Function
              int R = fPtr.Invoke("ABCabc");
      
              Console.WriteLine(R);
      
              fPtr = new StringFuncDelDT(StringFunctions.GetUpChar);
      
              R = fPtr.Invoke("ABCabc");
      
              Console.WriteLine(R);
      
              //fPtr = new StringFuncDelDT(StringFunctions.GetFullName);
              ///Compiler Enforce Delegate Signature
      
              StringFunctions SObj = new StringFunctions() { Ch = 'z' };
      
              StringFunctions SObj02 = new StringFunctions() { Ch = 'c' };
      
              ///Object Methos
              fPtr = new StringFuncDelDT(SObj.GetCharNum);
      
              R = fPtr("Abczyz");
              /// fptr.Invoke("Abczyz");
      
              Console.WriteLine(R);
          }
      }
      
      class StringFunctions
      {
          public static int GetLenght (string Str) { return Str?.Length ?? -1; }
      
          public char Ch { get; set; }
          internal int GetCharNum (String Str)
          {
              int Coutner = 0;
              for (int i = 0; i < Str?.Length; i++)
                  if (Str[i] == this.Ch) Coutner++;
              return Coutner;
          }
      
          public static int GetFullName ( string FName , string LName) 
          { return FName.Length + LName.Length; }
      
          public static int GetUpChar(string Str)
          {
              int Coutner = 0;
              for (int i = 0; i < Str?.Length; i++)
                  if (Char.IsUpper( Str[i]) ) Coutner++;
              return Coutner;
          }
      }
      

    • Delegate Ex2 →
      • Ordinary way to make methods →

        class Program
        {
        
            public static List<int> FindOdd(List<int> Lst)
            {
                List<int> oLst = new List<int>();
        
                for (int i = 0; i < Lst?.Count; i++)
                    if (Lst[i] % 2 == 1)
                        oLst.Add(Lst[i]);
        
                return oLst;
            }
        
            public static List<int> FindEven(List<int> Lst)
            {
                List<int> oLst = new List<int>();
        
                for (int i = 0; i < Lst?.Count; i++)
                    if (Lst[i] % 2 == 0)
                        oLst.Add(Lst[i]);
        
                return oLst;
            }
        
            public static List<int> FindDivBy7(List<int> Lst)
            {
                List<int> oLst = new List<int>();
        
                for (int i = 0; i < Lst?.Count; i++)
                    if (Lst[i] % 7 == 0)
                        oLst.Add(Lst[i]);
        
                return oLst;
            }
        
            static void Main(string[] args)
            {
                List<int> iLst = Enumerable.Range(0, 100).ToList();
        
                List<int> Lst2;
        
                Lst2 = FindOdd(iLst);
        
                Lst2 = FindEven(iLst);
        
                Lst2 = FindDivBy7(iLst);
        
                foreach (var item in Lst2)
                    Console.Write($"{item} , ");
        
                Console.WriteLine();
            }
        }
        
      • Last code using delegate →

        public delegate bool CondDelDT(int X);
        
        class Program
        {
          public static List<int> FindCondition(List<int> Lst, CondDelDT CondFunc)
          {
              List<int> oLst = new List<int>();
        
              for (int i = 0; i < Lst?.Count; i++)
                  if (CondFunc?.Invoke(Lst[i]) == true)
                      oLst.Add(Lst[i]);
        
              return oLst;
          }
        
          static void Main(string[] args)
          {
              List<int> iLst = Enumerable.Range(0, 100).ToList();
        
              List<int> Lst2;
        
              CondDelDT fPtr = new CondDelDT(ConditionFunctions.ChkOdd);
        
              fPtr = ConditionFunctions.ChkEven;
        
              Lst2 = FindCondition(iLst, fPtr);
        
              Lst2 = FindCondition(iLst, ConditionFunctions.ChkDivBy7);
        
              Lst2 = FindCondition(iLst, ConditionFunctions.ChkGrt50);
        
              foreach (var item in Lst2)
                  Console.Write($"{item} , ");
        
              Console.WriteLine();
          }
        }
        
        class ConditionFunctions
        {
            public static bool ChkOdd(int x) { return x % 2 == 1; }
            public static bool ChkEven(int x) { return x % 2 == 0; }
            public static bool ChkDivBy7(int x) { return x % 7 == 0; }
        
            public static bool ChkGrt50(int x) { return x > 50; }
        
        }
        

    • Delegate Ex3 →