stackoverflow1453

Programming&&Games&&Life

Archive for April, 2017

params keyword

using System;

namespace _25_04_2017_c
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var sum1 = SumParameters();
            var sum2 = SumParameters(1, 2, 3);
            var sum3 = SumParameters(3, 4, 5, 6);
            var sum4 = SumParameters(12, 23, 4);

            Console.WriteLine(sum1);
            Console.WriteLine(sum2);
            Console.WriteLine(sum3);
            Console.WriteLine(sum4);

            Console.ReadKey();
        }

        private static int SumParameters(params int[] degerler)
        {
            var result = 0;

            foreach (var variable in degerler)
                result = result + variable;
            return result;
        }
    }
}

topl

Anonymous Methods – 2

using System;

namespace _25_04_2017_b
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            //using anonymous function in delegate
            Del mydel = delegate(int a, int b) { return a + b; };

            var result = mydel(2, 3);

            Console.WriteLine(result);
            Console.ReadKey();
        }

        private delegate int Del(int x, int y);
    }
}

Anonymous Methods – 1

using System;

namespace _25_04_2017_a
{
    internal delegate void Del();

    internal class Program
    {
        private static void Main(string[] args)
        {
            //anonymous method
            Del a = delegate { Console.WriteLine("Hello"); };
            a();
            Console.ReadKey();
        }
    }
}

Delegate 3 – 7

using System;

namespace _24_04_2017_c
{
    public delegate int Del(ref int x);

    internal class Class1
    {
        public int Add2(ref int x)
        {
            return x += 2;
        }

        public int Add3(ref int x)
        {
            return x += 3;
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var c = new Class1();
            Del mydel = c.Add2;
            mydel += c.Add3;
            mydel += c.Add2;

            var x = 3;

            Console.WriteLine(mydel(ref x));

            Console.ReadLine();
        }
    }
}

10

Delegate 3 – 6

using System;

namespace _24_04_2017_c
{
    public delegate int Del();

    internal class Class1
    {
        private int _val = 3;

        public int Add2()
        {
            return _val += 2;
        }

        public int Add3()
        {
            return _val += 3;
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var c = new Class1();
            Del mydel = c.Add2;
            mydel += c.Add3;
            mydel += c.Add2;

            Console.WriteLine(mydel());

            Console.ReadLine();
        }
    }
}

Delegate 3 – 5

using System;

namespace _24_04_2017_c
{
    public delegate int Del(int k);

    class Class1
    {
        public int Add2(int value)
        {
            return value += 2;
        }

        public int Add3(int value)
        {
            return value += 3;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1();
            Del mydel = c.Add2;
            mydel += c.Add3;
            mydel += c.Add2;

            int result = 3;

            foreach (var func in mydel.GetInvocationList())
            {
                result = (int) func.DynamicInvoke(result);
            }

            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
}

Delegate 3 – 4

using System;

namespace _24_04_2017_b
{
    public delegate void Del();

    internal class MixedClass1
    {
        public void Print3()
        {
            Console.WriteLine("Print3  ---  Instance");
        }

        public static void Print4()
        {
            Console.WriteLine("Print4  ---  Static");
        }
    }


    internal class Program
    {
        private static void Main(string[] args)
        {
            var p = new Program();
            var mc2 = new MixedClass2();
            var mc1 = new MixedClass1();

            Del d = mc2.Print1;
            d += MixedClass2.Print2;
            d += mc2.Print1;
            d += mc1.Print3;
            d += MixedClass1.Print4;

            if (d != null) d();
            else Console.WriteLine("Delegate is null");
            Console.ReadKey();
        }
    }
}

using System;

namespace _24_04_2017_b
{
    internal class MixedClass2
    {
        public void Print1()
        {
            Console.WriteLine("Print1  ---  Instance");
        }

        public static void Print2()
        {
            Console.WriteLine("Print2  ---  Static");
        }
    }
}

Instance.png