LICT .NET Training | Kamal Protap Sir | Part 3

09/11/2017 vids

Property:

using System;

namespace LinqOperators
{
    class Student
    {
        private int Sid;
        private string sname;


        public int StudentID
        {
            get { return Sid; }
            set { Sid = value; }
        }

        //public void Display()
        //{
        //    Console.WriteLine(Sid);
        //}

        public string StudentName
        {
            get { return sname;  }
            set { sname = value;  }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Student obj=new Student();
            obj.StudentID = 10;
            obj.StudentName = "Zaki";
            Console.WriteLine(obj.StudentName);
            Console.WriteLine(obj.StudentID);
            //obj.Display();
            Console.ReadKey();
        }
    }
}

Automatic Property:

//auto implemented property

using System;

namespace LinqOperators
{
    class Student
    {
        public int Sid { private get; set; }
        public string Sname { get; set; }


       
    }

    class Program
    {
        static void Main(string[] args)
        {
            
            Student obj=new Student();
            obj.Sid = 10;
            obj.Sname = "Zaki";
            Console.WriteLine(obj.Sid+" "+obj.Sname);
            Console.ReadKey();
        }
    }
}

Delegates:

using System;

namespace Delegates
{
    delegate int Caluclation(int Fnum, int Snum);

    class Delegates
    {
        public static int Add(int Firstnum, int Secondnum)
        {
            return Firstnum + Secondnum;
        }

        public static int Sub(int Firstnum, int Secondnum)
        {
            return Firstnum - Secondnum;
        }

        public static string Display(string Sname)
        {
            return Sname;
        }

        public static void Main(string[] args)
        {
            Caluclation calc = Add;
            Console.WriteLine(calc.Invoke(3, 4));
            calc = Sub;
            Console.WriteLine(calc.Invoke(10, 5));

            Console.ReadKey();
        }
    }
}

Lambda Expression with FUNC:

using System;


namespace LamdawithDelegates
{
    delegate int MyDelegatesss(int firstNum, int secondNum);

    class Program
    {
        public static int Add(int firstNum, int secondNum)
        {
            return firstNum + secondNum;
        }
        static void Main(string[] args)
        {
            MyDelegatesss calc = Add;
            Console.WriteLine(calc.Invoke(5,4));
            Console.ReadKey();

        }
    }
}

Some logics:

//delegate example

delegate int MyDelegate(int firstNumber, int secondNumber);
MyDelegate myDelegate=(firstNumber,secondNumber)=>firstNumber+secondNumber
int result=myDelegate(3,4);
Console.WriteLine(result);

Lambda using Function:

//lambda using FUNC
Func<int, int , int> funcdel=(a,b)=>{return a*b};
int Result=funcdel(30,20);
Console.WriteLine(Result);

Multicast Delegates:

A delegate instance holding the multiple method at a time is multicast delegate.
But the rule is that all the method must be altered because all method in a single instance they remain present. Multiple reference method at a  time.

using System;

namespace MulticastDelegates
{
    public delegate void MultiDelegate(int a, int b);

    public class SampleClass
    {
        public static void Add(int x, int y)
        {
            Console.WriteLine("Addition value:"+(x+y));

        }

        public static void Sub(int x, int y)
        {
            Console.WriteLine("Subtraction Value:"+(x-y)); 
        }

        public static void Mul(int x, int y)
        {
            Console.WriteLine("Mutiplication Value:"+(x*y));
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            MultiDelegate del = SampleClass.Add;
            del += SampleClass.Sub;
            del += SampleClass.Mul;
            del(10, 5);
            Console.WriteLine();
            del -= SampleClass.Mul;
            del -= SampleClass.Sub;
            del(10, 5);
            Console.ReadKey();
        }
    }
}

Anonymous Method:
Unnamed function. In the bracket we are writing the logic. Unnamed method we are writing the logic. Unanmed method.

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

namespace AnonynemousMethod
{
    public delegate double Areapointer(int r);
    class Program
    {
        static void Main(string[] args)
        {
            Areapointer point=new Areapointer(
                delegate(int r)
                {
                    return 3.14 * r * r;
                }
                );

            double area = point(20);

            Console.WriteLine(area);

            Console.ReadKey();
        }
    }
}

 

The same AnonymousMethod work can be done in a single line with this lambda operation:

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

namespace AnonynemousMethod
{
    public delegate double Areapointer(int r);
    class Program
    {
        static void Main(string[] args)
        {
            Areapointer point = r => 3.14 * r * r;

            double area = point(20);

            Console.WriteLine(area);

            Console.ReadKey();
        }
    }
}

=> This is known as goes to operator and it is similar to AnonymousMethod()

Linq also using lambda not only Delegate

Func:

Func is a readymade function where the first data type is for input and the second one is for output. It does not need any delegate to declare.

using System;

namespace FuncwithLambda
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<double, double> area = r => 3.14 * r * r;
            double result = area(20);
            Console.WriteLine(result);
            Console.ReadKey();
        }
    }
}

Action:
Action does not support any return type:

using System;


namespace Action
{
    class Program
    {
        static void Main(string[] args)
        {
            Action<String> area = sttr => Console.WriteLine(sttr);
            area("zaki");

        }
    }
}

 

 

 

 

 

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.