stackoverflow1453

Programming&&Games&&Life

Boxing

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

namespace _07_06_2017_c
{
    class Program
    {
        static void Main(string[] args)
        {
            //Boxing is the implicit conversion of value types to reference types.
            int a = 5;
            object o = a;

            Console.WriteLine("a is equal to: {0}, o is equal to: {1}", a, o);
            a = 4;
            Console.WriteLine("a is equal to: {0}, o is equal to: {1}", a, o);
            o = 6;
            Console.WriteLine("a is equal to: {0}, o is equal to: {1}", a, o);

            Console.ReadKey();
        }
    }
}

Unsafe Cast

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

namespace _07_06_2017_a
{
    class A { public int field1; }
    class B : A { public string field2; }

    class Program
    {
        static void Main(string[] args)
        {
            A var1 = new A();

            var1.field1 = 5;

            B var2 = new B();

            var2.field1 = 14;
            var2.field2 = "FLD2";

            Console.WriteLine("var1.field1 = {0}", var1.field1); //5
            Console.WriteLine("var1.field1 = {0}", var2.field1); //FLD2
            Console.WriteLine("var2.field1 = {0}", var2.field2); //14


            //B var4 = (B)var1; //Runtime Exception Occurs here! (Because of Unsafe Invalid Cast)

            A var5 = (A)var2;
            Console.WriteLine("var5.field1={0}", var5.field1); //14

            Console.ReadKey();
        }
    }
}

Interface – 3 (Interfaces can Inherit other Interfaces)

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

namespace ConsoleApplication12
{
    interface IInterface { int getData(); }
    interface IInterface2 { void setData(int x); }
    interface IInterface3 : IInterface, IInterface2 { }

    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            a a1 = new a();

            a1.setData(3);
            a1.getData();

            Console.WriteLine(a1.getData());
            Console.ReadKey();
        }
    }

    class a : IInterface3
    {
        int p;
        public int getData() { return p; }
        public void setData(int x) { p = x; }
    }
}

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