امروز بعد از مدتها اومدم تا يه مطلبي رو بزارم و برم چون بايد زود برم به خاطر همين زياد در موردش توضيحات نميزارم چون توضيحات توي كدها هستش

سربارگذاري عملگرها رو اكثر دوستان بايد توي سي شارپ بلد باشند اما امروز من اومدم تا سربارگذاري Iplicit & Explicit  رو بهتون بگم و برم كدا توي ادامه مطلب:







using System;

namespace Overloding
{
    class TimeHHMMSS
    {
        //001: Parts of the time class
        public int m_hour;
        public int m_minute;
        public int m_sec;

        //002: Default constructor for the class
        public TimeHHMMSS()
        {
            m_hour = 0;
            m_minute = 0;
            m_sec = 0;
        }

        //003: Overloaded Construtor
        public TimeHHMMSS(int hr, int min, int sec)
        {
            m_hour = hr;
            m_minute = min;
            m_sec = sec;
        }

        //004: Every class that we create has Object as the base class. Override the ToString
        public override string ToString()
        {
            return string.Format("{0}:{1}:{2}", m_hour, m_minute, m_sec);
        }

        //005: Implicit Conversion Operator. Coversion from int to TimeHHMMSS
        public static implicit operator TimeHHMMSS(int totalSeconds)
        {   
            //005_1 : Declarations
            int hour, min, seconds;
            int RemainingSeconds;
            TimeHHMMSS returnobject = new TimeHHMMSS();

            //005_2: Calculate Seconds
            seconds = totalSeconds % 60;
            returnobject.m_sec = seconds;

            //005_3: Calculate Minutes
            RemainingSeconds = totalSeconds - seconds;
            min = RemainingSeconds % ( 60 * 60 ); //Calculated minutes in terms of seconds
            returnobject.m_minute = min / 60 ;

            //005_4: Calculate Hours
            RemainingSeconds =  totalSeconds - ( returnobject.m_minute * 60 + returnobject.m_sec );
            hour = RemainingSeconds % (60 * 60 * 24 ); // Hour in Seconds
            returnobject.m_hour = hour / 3600;

            return returnobject;
        }

        //006: Implicit Conversion Operator. Conversion from TimeHHMMSS to integer
        public static implicit operator int(TimeHHMMSS time)
        {
            return (time.m_hour * 60 * 60) + (time.m_minute * 60 ) + time.m_sec ;
        }

        //007: Explicit Conversion Operator. Conversion from float to TimeHHMMSS.
        public static explicit operator TimeHHMMSS(float totalSeconds) // We don't want to handle fraction in Seconds (Micro Sec or nano Sec stc)
        {
            TimeHHMMSS returnobject = new TimeHHMMSS();
            returnobject = (int) totalSeconds; //Makes a call to implicit convertion : int->TimeHHMMSS
            return returnobject;
        }

        //008: Operator Overloading +. Binary Operator.
        public static TimeHHMMSS operator+ (TimeHHMMSS operandLH, TimeHHMMSS OperandRH)
        {
            //008_1: Get the Seconds and Add.
            int totalSeconds, LHS_Seconds, RHS_Seconds;
            LHS_Seconds = operandLH;        //Implicit convertion: Time -> int
            RHS_Seconds = OperandRH;        //Implicit convertion: Time -> int
            totalSeconds = LHS_Seconds + RHS_Seconds;

            //008_2:    Return the Seconds. Required return type is TimeHHMMSS,
            //            what we return is integer. Again one more implicit conevrtion in reverse
            return totalSeconds; //Implicit convertion: int -> Time
        }

        [STAThread]
        static void Main(string[] args)
        {
            //Usage_000: Create the Time Class
            TimeHHMMSS t1 = new TimeHHMMSS(0,20,10);
            Console.WriteLine("The time is: {0}", t1.ToString());

            //Usage_001: Implicit Conversion Operator[ int to Time ]
            TimeHHMMSS t2 = new TimeHHMMSS();
            t2 = 33175;        //=> Implicit converion for int to Time Takes place
            Console.WriteLine("The time is: {0}", t2.ToString());

            //Usage_002: Implicit  Convertion Operator[ Time to int ]
            int timeinSeconds;
            timeinSeconds = t2;        //=> Implicit converion from Time to Integer Takes place
            Console.WriteLine("The time {0} in Seconds (2)", t2.ToString(),timeinSeconds );

            //Usage_003: Explicit Conversion Operator. [Here I am asking convert my float to an Time Object]
            TimeHHMMSS t3 = (TimeHHMMSS) 23213.67f;
            Console.WriteLine("The time is: {0}", t2.ToString());

            //Usage_004_A: Overloaded + operator. [ Put a Break point here & Step-in. You will learn more]
            // [Tag1]  a) Time->int implicit for t1, t2. b) add two int c)return added int. int->Time implicit
            TimeHHMMSS t4 = t1 + t2;
            Console.WriteLine("The time is: {0}", t4.ToString());

            //[Tag2] a) int->Time Conversion. Read the Comment [Tag1]
            t4 = t4 + 4215;
            Console.WriteLine("The time is: {0}", t4.ToString());

            //a)Float->Time Conversion. Explicit. [Read Tag2]
            t4 = t4 + (TimeHHMMSS) 54321.456f ;

        }
    }
}