public static void Main()
        {
            DelegateClass  dc = new DelegateClass();
            SampleDelegate sd = new SampleDelegate(dc.DelegateMethod);

            sd();
        }
示例#2
0
        public void DemoDelegate()
        {
            Console.WriteLine("-----------------Delegate Example-----------------");
            DelegateClass delegateClass = new DelegateClass();

            delegateClass.PrintNumSimpleDelegate();
            delegateClass.PrintNumMulticastDelegate();
        }
示例#3
0
    public static void Main()
    {
        DelegateClass del = delegate {
            Console.WriteLine("Running anonymous method");
        };

        del();
    }
示例#4
0
    public static void Main()
    {
        DelegateClass del      = (DelegateClass)AddOne + (DelegateClass)AddTwo + (DelegateClass)AddOne;
        int           valCount = 0;
        int           refCount = 0;

        del(valCount, ref refCount);
        Console.WriteLine("Value count = {0}", valCount);     // 0
        Console.WriteLine("Reference count = {0}", refCount); // 4
    }
示例#5
0
        public void Test1()
        {
            DelegateClass dc = new DelegateClass();
            calculator    c1 = new calculator(dc.add);
            calculator    c2 = new calculator(dc.multiply);

            c1(20);
            Assert.AreEqual(dc.getNum(), 120);
            c2(3);
            Assert.AreEqual(dc.getNum(), 360);
        }
示例#6
0
    public static void Main()
    {
        DelegateClass delegateClass = new DelegateClass();

        // Creating a delegate before using
        MyDelegate myDelegate = delegateClass.DelegateFunction;

        delegateClass.Function(myDelegate);

        // Shorter version
        delegateClass.Function(delegateClass.DelegateFunction);
    }
示例#7
0
        public static void Test()
        {
            Console.WriteLine("### Adapter");

            Console.WriteLine("--- 既存のクラスをそのまま使う");
            var kappa = new Kappa();

            kappa.Cry();

            Console.WriteLine("--- 継承して新しいインターフェースから呼び出す");
            var kappa2 = new DeriveClass();

            kappa2.NewCry();

            Console.WriteLine("--- 委譲して新しいインターフェースから呼び出す");
            var kappa3 = new DelegateClass();

            kappa3.NewCry();
        }
示例#8
0
        private bool SearchForWiki()
        {
            DelegateClass del = lyricWiki.getSong;

            IAsyncResult ar = del.BeginInvoke(this.artist, this.title, null, null);

            while (noOfTries < 9)
            {
                // If the user has aborted stop the search and return (false)
                if (Abort || lyricSearch.SearchHasEnded)
                {
                    return(false);
                }
                else if (ar.AsyncWaitHandle.WaitOne(0, true))
                {
                    lyricsResult = del.EndInvoke(ar);

                    string   lyric   = lyricsResult.lyrics;
                    Encoding iso8859 = Encoding.GetEncoding("ISO-8859-1");
                    lyricsResult.lyrics = Encoding.UTF8.GetString(iso8859.GetBytes(lyricsResult.lyrics));
                    break;
                }
                else
                {
                    // if we don't allow this pause of 2 sec the webservice behaves in a strange maneur
                    Thread.Sleep(2000);
                }
                ++noOfTries;
            }

            if (lyricsResult != null && IsLyric(lyricsResult.lyrics))
            {
                return(true);
            }
            else
            {
                noOfTries = 0;
                return(false);
            }
        }
示例#9
0
        private static void CallDelegate()
        {
            Console.WriteLine("Delegate Tests");
            DelegateClass dc = new DelegateClass();

            MulticastDelegateClass mdc = new MulticastDelegateClass();

            //Methods call in the order they were added to delegate instance (+= or -= create a new istance of delegate)
            //If no methods are assigned, delegate istance = null
            //If return type no void and more than one method is assigned to delegate, it return only last called method result
            //(preceding methods are still called, but their only return values are discarded)
            ProgressReporter pr = mdc.WriteProgressToConsole;

            pr += mdc.WriteProgressToFile; //combine
            //pr -= mdc.WriteProgressToConsole; //remove
            var methodClass = pr.Target;   //Method class instance (only if method assigned to delegate is an instance method)
            var method      = pr.Method;   //Method

            mdc.HardWork(pr);

            Console.WriteLine("Generic Delegate");
            GenericDelegate gd = new GenericDelegate();
        }
示例#10
0
        private bool searchForWiki(string artist, string title)
        {
            DelegateClass del = lyricWiki.getSongResult;

            IAsyncResult ar = del.BeginInvoke(this.artist, this.title, null, null);

            while (noOfTries < 15)
            {
                // If the user has aborted stop the search and return (false)
                if (Abort)
                {
                    return(false);
                }

                if (ar.AsyncWaitHandle.WaitOne(0, true))
                {
                    lyricsResult = del.EndInvoke(ar);
                    break;
                }
                else
                {
                    // if we don't allow this pause of 2 sec the webservice behaves in a strange maneur
                    Thread.Sleep(2000);
                }
                ++noOfTries;
            }

            if (lyricsResult != null && isLyric(lyricsResult.lyrics))
            {
                return(true);
            }
            else
            {
                noOfTries = 0;
                return(false);
            }
        }
示例#11
0
    static void Main(string[] args)
    {
        #region Interface - Property caller
        Console.WriteLine();
        // Implementing property : Setting value to the property in IF extended class
        MyClass objMC = new MyClass();
        objMC.Name = "shiva";
        Console.WriteLine("Name provided (Interface) : " + objMC.Name);
        Console.WriteLine();

        IFTest objT = new IFTest();
        // Calling SaveData would be ambiguous (since both Interface has same method name). Compiler calls both implementation
        string strIsIdatastore   = (objT is IDataStore) ? "Yes" : "No";
        string strIsISerializabl = (objT is IDataStore) ? "Yes" : "No";
        objT.SaveData();
        Console.WriteLine("Is IDataStore called : " + strIsIdatastore);
        Console.WriteLine("Is ISerializabl called : " + strIsISerializabl);
        Console.WriteLine();

        // To call a specific member of the interface casting can be done accordingly
        ((ISerializabl)objT).SaveData();
        ((IDataStore)objT).SaveData();
        Console.WriteLine();

        EditBox edit = new EditBox();
        edit.SaveData();
        Console.WriteLine();
        #endregion

        #region Abstract class - sum caller
        // Abstract class cannot be instantiated
        AddClass objAC = new AddClass();
        int      sum   = objAC.Add(2, 3);
        Console.WriteLine("Sum of two nos (using Abstract Class) : " + sum);
        Console.WriteLine();
        #endregion

        #region Polymorphism - callign base class/derived class methods
        // Polymorphism : Calling base class method
        BaseClassPoly objBCP         = new BaseClassPoly();
        string        strWelcomeBase = objBCP.Welcome();
        Console.WriteLine(strWelcomeBase);
        Console.WriteLine();

        // calling derived class & methods
        DerivedClassPoly objDCP = new DerivedClassPoly();
        string           strWel = objDCP.Welcome("shiva");
        Console.WriteLine(strWel);
        Console.WriteLine(objDCP.DateDetails());
        Console.WriteLine(objDCP.Welcome());
        Console.WriteLine();
        #endregion

        #region Indexer - set & get for int & string indexers
        // Indexer. setting and getting values
        IndexerClass objIC = new IndexerClass();
        objIC[1] = 1;
        objIC[0] = 0;
        objIC[2] = 2;
        // This value cannot be assigned as indexer size is only 3 (zero based)
        // Inside indexer setter : Value would be assignd as Zero as the index is outside bounds of the array
        objIC[4] = 10;

        for (int i = 0; i <= 5; i++)
        {
            Console.WriteLine("Indexer value for objIC[" + i + "] : " + objIC[i]);
        }

        // Indexer can be overloaded. multiple indexers can co-exist in a class
        // below indexer returns String output for the day provided
        Console.WriteLine();
        string dayCount = objIC["FRI"];
        Console.WriteLine("Indexer day count for Friday : " + dayCount);
        Console.WriteLine();
        #endregion

        #region Delegates - sum/sub & multicast
        // delegates are references to method
        // Create an instance of the class & pass the method as reference to delegate object
        DelegateClass objDC = new DelegateClass();
        // Method can be passed as reference to delegate in below way also or can be assigned directly.
        // AddSubDelegate objAddSubDel = new AddSubDelegate(objDC.Add);
        AddSubDelegate objAddSubDel = objDC.Add;
        int            delsum       = objAddSubDel(1, 2);
        Console.WriteLine("Delegate := Sum & sub of 1 & 2 : " + delsum);

        // Multicast delegates
        Console.WriteLine();
        objDC = new DelegateClass();
        AddSubDelegate objAddSubMultiDel = null;
        objAddSubMultiDel  = new AddSubDelegate(objDC.Add);
        objAddSubMultiDel += new AddSubDelegate(objDC.Sub);

        int delSumSub = objAddSubMultiDel(1, 2);
        Console.WriteLine("Multicast Delgate : Add & sub of 1,2 methods called: " + delSumSub);
        //removing sub method
        objAddSubMultiDel -= new AddSubDelegate(objDC.Sub);
        Console.WriteLine("Multicast Delgate : removing sub of 1,2 method : " + objAddSubMultiDel(1, 2));
        Console.WriteLine();
        #endregion

        #region Events
        // call event class & assign ChangeEvent with the corresponding handler
        EventClass objEC = new EventClass();
        objEC.ChangeEvent += new MyDelegate(objEC.ChangeEventMethod);
        // ChangeEvent is called everytime when the setter is called in EventClass
        objEC.Price = 10;
        objEC.Price = 20;
        Console.WriteLine();
        #endregion

        #region Value & Reference
        ValueAndRefClass objVAR = new ValueAndRefClass();
        // Implicit Value types (like int) are always passed by value
        int a;
        objVAR.DoWork(out a);
        Console.WriteLine("Value of A is :" + a);
        Console.WriteLine();

        int b = 3;
        Console.WriteLine("Value of B before REF is :" + b);
        objVAR.DoWorkB(ref b);
        Console.WriteLine("Value of B after REF is :" + b);
        Console.WriteLine();

        // Object Types are impicitly passed by reference
        int[] nums = { 1, 3, 5 };
        objVAR.DoWorkObject(nums);

        int count = 0;
        foreach (int n in nums)
        {
            Console.WriteLine("Value of a[{0}] is {1}", count++, n);
        }
        Console.WriteLine();
        #endregion
    }
示例#12
0
 /// <summary>
 /// Mouse is moved, left mouse button is pressed or none button is pressed
 /// </summary>
 /// <param name="richPictureBox"></param>
 /// <param name="e"></param>
 public virtual void OnMouseMove(RichPictureBox richPictureBox, MouseEventArgs e)
 {
     DelegateClass.GetDelegate().ChangeSysFunctionHandler?.Invoke();
 }
示例#13
0
 private extern static void Register(DelegateClass d_class, DelegateNew d_new, DelegateBang d_bang, DelegateFloat d_float, DelegateSymbol d_symbol, DelegatePointer d_pointer, DelegateList d_list, DelegateAnything d_anything, DelegateObject d_object);
示例#14
0
 private extern static void Register(DelegateClass d_class, DelegateNew d_new, DelegateBang d_bang, DelegateFloat d_float, DelegateSymbol d_symbol, DelegatePointer d_pointer, DelegateList d_list, DelegateAnything d_anything, DelegateObject d_object);