• Access Modifiers →

            class AccessModifier
            {
            //default access modifier 4 attribute is private
            int X; 
            // private + accessible for derived (child) classes
            private protected int M; ///C# 7.X Feature
            // accessable for derived classes
            protected int Y;
            // accessable in same project
            internal int Z;
            // accessable to inherited classes across the project
            protected internal int L;
            // public
            public int K;
            }
    

  • Casting operator overloading →

    public static explicit operator Person (Employee E)
    {
        var Names = E.Name.Split(' ');
    
        return new Person() { FName = Names[0], LName = Names[1] };
    }
    

  • Object Initializer →

    //Employee E = new Employee();
    //E.ID = 101;
    //E.Name = "Aly";
    //E.Salary = 7000;
    
    //Object Initializer
    Employee E = new Employee() { ID = 101, Name = "Aly", Salary = 7000 }; 
    

  • Constructor chaining →

    class Car
    {
        int id;
        string model;
        int currentSpeed;
        
        ///if no user defined ctor exists , 
        //Compiler will generate Empty paramterless ctor (Do Nothing)
        ///public Car () {}
        ///if  any userdefined ctor (with any Signature) exists , 
        //compiler will no longer generate empty paramterless ctor
    
        public Car(int _id , string _Model , int _cSpeed)
        {
            id = _id;
            model = _Model;
            currentSpeed = _cSpeed;
        }
    		// constructor chaining
        public Car( int _id , string _Model)
            :this(_id , _Model , 60)
        {
            //id = _id;
            //model = _Model;
            //currentSpeed = 60;
        }
    		
        public Car(int _id)
            :this(_id , "FIAT" , 60)
        {
            //id = _id;
            //model = "FIAT";
            //currentSpeed = 60;
        }
    
        ///Copy Ctor
        public Car(Car Oldc)
        {
            id = Oldc.id;
            model = Oldc.model;
            currentSpeed = Oldc.currentSpeed;
        }
    }
    

  • Static Attribute, Method, Constructor and Property →

    class Utility
    {
        public int X { get; set; }
        public int Y { get; set; }
    
        public Utility(int _X , int _Y)
        {
            X = _X;
            Y = _Y;
            //pi = 3.14; 
            ///Object Ctor is not a right place for Initializing Static Members
            Console.WriteLine("Ctor");
        }
    
        ///Static Ctor
        ///will be executed only Once per class lifetime (Program)
        ///can't specify return Datatype , Access modifier or Input -- 
        //Paramters for static Ctor
        ///cctor : static Ctor in il
        ///Max only one static ctor per class
        static Utility()
        {
            Console.WriteLine("Static Ctor");
            pi = 3.14;
        }
    
        public static double Cm2Inch (double Cm)
        {
            return Cm / 2.54;
        }
    
        public static double Inch2Cm (double Inch)
        {
            return Inch * 2.54;
        }
    
        static double pi;
        ///Static Attributes Allocate in Heap defore First usage 
        //to Class till end of program (or unreachable in case of reference Types)
        ///Static members initialized to default values by Default
    
        public static double PI { get { return pi; } }
    
        public static double CircleArea(double R)
        {
            return pi * R * R;
        }
    
    }
    

  • Static Class →

    ///Container to Only Static Members
    ///No Objects can be Created from this Class
    ///no Inheritance for Static Class
    static class Utility
    {
    }
    

  • Operator Overloading →

    ///Must be Static Member Function
    ///Can't overload [] or =
    class Complex
    {
    	   public int Real { get; set; }
         public int Imag { get; set; }
         
         public static Complex operator+ (Complex L , Complex R)
    		{
    		    return new Complex()
    		    {
    		        Real = (L?.Real??0) + (R?.Real??0),
    		        Imag = (L?.Imag??0) + (R?.Imag??0)
    		    };
    		}
    		
    		  public static Complex operator +(Complex L, int R)
          {
              return new Complex()
              {
                  Real = (L?.Real ?? 0) + R,
                  Imag = (L?.Imag ?? 0) 
              };
           }
           
            ///Can't Explicitly Overload Compund Operators (+= , -= , *= , .....)
            ///if you overload Original Operator (+) , 
            //Compund Operator will be supported by default
            //public static Complex operator+= ( Complex L , Complex R)
            //{
            //    throw new NotImplementedException();
            //}
            
            public static Complex operator - (Complex C)
            {
                return new Complex() { Real = -C.Real, Imag = -C.Imag };
            }
            
            ///Cater for Both Pre fix, Postfix versions
            public static Complex operator ++ (Complex C)
            {
                return new Complex() { Real = C.Real+1, Imag = C.Imag };
            }
            
            ///Compa. Operators Comes in Pairs
            public static bool operator > (Complex L , Complex R)
            {
                if (L.Real == R.Real)
                    return L.Imag > R.Imag;
                return L.Real > R.Real;
            }
            
            public static bool operator <(Complex L, Complex R)
            {
                if (L.Real == R.Real)
                    return L.Imag < R.Imag;
                return L.Real < R.Real;
            }
         
    }
    

  • Automatic Property →

    ///Automatic Property 
    ///Compiler will generate a hidden (private) Attribute and 
    //encapsulate it with Public Property
    
    public decimal TaxRate { get; }