UrbanPro
true

Learn C# .NET from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Understanding Delegate in C# .NET

P K Subudhi
14/05/2017 0 0

C# Delegate technique

Delegate is one of the fantastic tool & technique in C#.NET. Using deleagte technique the following benefits could be avaialed

  • One can define a method without name (Anonymous Method)
  • One can pass a Method as argument to another Method
  • Can join (or Add) two or more non-returnable methods methods
  • Core to combine event and event actions

Here is a simple example that demonstrates how delegates are being used in C#

Delegate used with instance method, static method, general named method and Anonymous method

Delegate Example

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

public delegate int DelType(int n);

namespace DelDemo

{

    class Num

    {

        int a;

        public Num() { a = 0; }

        public Num(int n) { a = n; }

        public void Disp() { Console.WriteLine("Data is " + a); }

        public int Power(int n) // Instance method

        {

            int r = 1;

            while ( n> 0)

            {

                r = r * a;

                n--;

            }

            return r;

        }

        static public int Rev(int t) // Static method

        {

            int r = 0;

          

            while (t > 0)

            {

                r = (r * 10) + (t % 10);

                t = t / 10;

            }

            return r;

        }

    }

    class Program

    {

        static int Fact(int n) // Named method

        {

            int f=1;

            while(n>1)f*=n--;

            return f;

        }

        static void Main(string[] args)

        {

            DelType act = delegate(int n)  //Anonymous method

            {

                int s = 0;

                while (n > 0)

                {

                    s = s + (n % 10);

                    n = n / 10;

                }

                return s;

            };

            Console.WriteLine("Sum of digit is "+act(234));

            Num p = new Num(5);

            act = Num.Rev;    // Using static method

            Console.WriteLine("Reverse number is " + act(567));

            act = Fact; // Using general named method

            Console.WriteLine("Factorial is " + act(5));

            act = p.Power; //Using instance method

            Console.WriteLine("Power is " + act(3));

            Console.ReadKey();

        }

    }

}

 

0 Dislike
Follow 0

Please Enter a comment

Submit

Other Lessons for You

What are various validator in ASP.NET 2.0 and features common in all validation control in ASP.NET
Control to validate an error message. Are common feature in all the controls.Various validators are requiredfeild validator ,compare validator ,regular expression validator,custom validator,range validator...

What Is The Difference In For...Loop And Foreach Loop?
Loop is very important in C# Programing language. Loop is call iteration statement, loops are used for executing the repeated task. 1. For...Loop: The for…loop executes a statement or a block...

A very brief introduction to .NET framework
.NET development has started in the year 2000 , a product of Microsoft. From the begining of .NET development till now, it has got many RAD facilities. Why .NET? developed for distributed computing...

OOPs Concepts
Session Objectives: Discuss the following: Drawbacks of traditional programming Need for Object: Oriented approach Object: Oriented techniques Discuss Object: Oriented Concepts Objects. Classes. Abstraction. Inheritance. Encapsulation. Polymorphism. Need...

Extension Methods in C#
Extension methods enables you to add methods to existing types without creating a new derived type, recompiling or otherwise modifying the original type. Extension methods are special type of static methods,...
X

Looking for C# .NET Classes?

The best tutors for C# .NET Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn C# .NET with the Best Tutors

The best Tutors for C# .NET Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more