stackoverflow1453

Programming&&Games&&Life

Archive for November, 2016

Event Example -Dozens and 21 Counter

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Eventt
{
    public delegate void Delegeytim();  //Create Delegate

    //Publisher
    class Artırıcı
    {
        public event Delegeytim Onikisaydim; //Create and Publish Event
        public event Delegeytim Yirmibirsaydim;

        public void Say()
        {
            for (int i = 1; i < 100; i++)
            {
                if (i % 12 == 0)
                {
                    Console.Write(i + "- ");
                    Onikisaydim(); //raise (Publisher raising event)                    

                }

                if (i % 21 == 0)
                {
                    Console.Write(i + "- ");
                    Yirmibirsaydim();
                }

                Thread.Sleep(200);
            }
        }
    }

    //Subscriber 
    class Dozens
    {
        int dozensCount = 0;
        int twentyOneCount = 0;

        public Dozens(Artırıcı a)
        {
            //Subscribe to the Event
            a.Onikisaydim += IncrementHandler12;
            a.Yirmibirsaydim += IncrementHandler21;
        }

        //EventhandlerMethod n1
        public void IncrementHandler12()
        {
            dozensCount++;
            Console.Write("12 Saydım, {0}\n", dozensCount);
        }

        //EventhandlerMethod n2
        public void IncrementHandler21()
        {
            twentyOneCount++;
            Console.Write("21 saydım,{0}\n", twentyOneCount);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Artırıcı Ar = new Artırıcı();
            Dozens doz = new Dozens(Ar);
            Ar.Say();
        }
    }
}

Reference and Value Types

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ValueType_RefType
{
    struct WebPageStruct
    {
        public string text;
    }

    class WebPageClass
    {
        public string text;
    }

    class Program
    {
        static void Main(string[] args)
        {
            //VALUE TYPE
            WebPageStruct text1, text2 = new WebPageStruct();
            text1.text = "Original Web Page Struct text.";
            text2 = text1;
            text2.text = "Copy Web Page Struct Test";

            //Output: Original Web Page Struct text
            Console.WriteLine(text1.text);

            //REFERENCE TYPE
            WebPageClass text3 = new WebPageClass();
            text3.text = "Original Web Page Class text";
            WebPageClass text4 = text3;
            text4.text = "Copy Web Page Class Text";

            //Output: Copy Web Page Class Text  
            Console.WriteLine(text3.text);
            
        }
    }
}

delegate 13-1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Deleg
{
    public delegate void Mydel(int y);

    class Program
    {
        public static void PrintLow(int val) { Console.WriteLine("{0}: Low Value", val); }
        public static void PrintHigh(int val) { Console.WriteLine("{0}: High Value", val); }

        static void Main(string[] args)
        {
            Mydel m = null;  //if you dont assign a value beforehand this gives error.

            Random r = new Random();
            int number = r.Next(99);

            if (number < 50) { m = new Mydel(PrintLow); }
            if (number >= 50) { m = new Mydel(PrintHigh); }

            m(number);
        }
    }
}

low