stackoverflow1453

Programming&&Games&&Life

Archive for May, 2017

Interface – 2

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

namespace DS_373
{
    interface IMyInterface1 //interface declaration can have access modifier
    {
        void Print(string s); //Interface methods can not have acccess modifier. They are public by default
    }

    public class Myclass : IMyInterface1
    {
        public void Print(string s)
        {
            Console.WriteLine("Calling through: {0}", s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Myclass mc = new Myclass();
            mc.Print("Object");

            IMyInterface1 i1 = (IMyInterface1)mc;
            i1.Print("Interface");

            IMyInterface1 i2 = mc as IMyInterface1;
            if (i2 != null) { i2.Print("Interface using as"); };
                        
            Console.ReadKey();
        }
    }
}

Interfaces – 1

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

namespace DS_373
{
    interface IMyInterface1 //interface declaration can have access modifier
    {
        void Print(string s); //Interface methods can not have acccess modifier. They are public by default
    }

    public class Myclass : IMyInterface1
    {
        public void Print(string s)
        {
            Console.WriteLine("Calling through: {0}", s);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Myclass mc = new Myclass();
            mc.Print("dsadsa");
            Console.ReadKey();
        }
    }
}

Publisher Subscriber Event Firing

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

namespace ConsoleApplication10
{

    class Sayici
    {
        public event EventHandler Counted12;

        public Sayici() { }

        public void Say()
        {
            for (int i = 0; i < 100; i++)
            {
                if (i % 12 == 0)
                {
                    if (Counted12 != null)
                    {
                        Counted12(this, null);
                        Console.WriteLine(i);
                    }
                }
            }
        }
    }


    class Abone
    {
        public int counter12;

        public Abone(Sayici s)
        {
            s.Counted12 += Artir12Sayac;
        }

        public void Artir12Sayac(Object o, EventArgs e)
        {
            counter12++;
            Console.WriteLine("12 ile karşılaşıldı ve artırıldı");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Sayici s = new Sayici();
            Abone a = new Abone(s);
            s.Say();
            Console.WriteLine("Toplam karşılaşılan 12 ile bölünebilen sayısı: {0}", a.counter12);
            Console.ReadKey();

        }
    }
}

Extensions I use for Visual Studio Enterprise 2015

 

Supercharger,

Codinion,

ReSharper

 

addons

Events 2 – Publisher Subscriber with System.EventHandler

using System;

//Dozen Counter
namespace _08_05_2017_a
{
    internal class Publisher
    {
        public event EventHandler PrimeNumberEncountered;

        public void StartCount()
        {
            for (var i = 2; i < 100; i++)
            {
                var divCount = 0;

                for (var j = 1; j < i; j++)
                    if (i % j == 0)
                        divCount++;

                if (divCount < 2)
                {
                    if (PrimeNumberEncountered != null) PrimeNumberEncountered(this, null);
                    Console.WriteLine(i);
                }
            }
        }
    }


    internal class Subscriber
    {
        public int PrimeCount;

        public Subscriber(Publisher p)
        {
            p.PrimeNumberEncountered += IncrementPrimeCounter;
        }

        public void IncrementPrimeCounter(object o, EventArgs e)
        {
            PrimeCount++;
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var y1 = new Publisher();
            var a1 = new Subscriber(y1);

            y1.StartCount();

            Console.WriteLine("--");
            Console.WriteLine(a1.PrimeCount);
            Console.ReadKey();
        }
    }
}

Events – 1 (Publisher – Subscriber Pattern with Prime Numbers)

using System;

//Dozen Counter
namespace _08_05_2017_a
{
    internal class Publisher
    {
        public delegate void Del();

        public event Del PrimeNumberEncountered;

        public void StartCount()
        {
            for (var i = 2; i < 1000; i++)
            {
                var divCount = 0;

                for (var j = 1; j < i; j++)
                    if (i % j == 0)
                        divCount++;

                if (divCount < 2)
                {
                    if (PrimeNumberEncountered != null) PrimeNumberEncountered();
                    Console.WriteLine(i);
                }
            }
        }
    }


    internal class Subscriber
    {
        public int PrimeCount;

        public Subscriber(Publisher p)
        {
            p.PrimeNumberEncountered += IncrementPrimeCounter;
        }

        public void IncrementPrimeCounter()
        {
            PrimeCount++;
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var y1 = new Publisher();
            var a1 = new Subscriber(y1);

            y1.StartCount();

            Console.WriteLine("--");
            Console.WriteLine(a1.PrimeCount);
            Console.ReadKey();
        }
    }
}

Anonymous Methods – Lambda Expressions

using System;

namespace _05_05_2017_a
{
    internal class Program
    {
        public delegate void Mydel();

        private static void Main(string[] args)
        {
            Mydel del1;
            Mydel del2;

            {
                var x = 5;
                //Anonymous method
                del1 = delegate { Console.WriteLine(x); };
                //Lambda Expression
                del2 = () => { Console.WriteLine("Lambda expression working."); };
            }

            //Console.WriteLine(x);
            del1();
            del2();
            Console.ReadKey();
        }
    }
}