.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Collections >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Use Multiple Cast Delegates in C# .net

Posted By:Narayanan       Posted Date: July 31, 2012    Points: 40    Category: Collections    URL: http://www.dotnetspark.com  

Multiple cast Delegates: ability to invoke multiple methods by using delegate. methods does not have return type and all methods need to have same signature.
 

First create a class file for using Method in Delegate.

Class name is  :Mathamatic.cs
Function Names are :
 Add,Sub,Mul,Divid

Functions are given below:


public static string Add(int i, int j)
        {
            return "Sum of  I and J  is " + " " + ":" + (i + j);
        }
        public static string Sub(int i, int j)
        {
            return "Sub of  I and J  is " + " " + ":" + (i - j);
        }
        public static string Mul(int i, int j)
        {
            return "Mul of  I and J  is " + " " + ":" + (i * j);
        }
        public static string Divi(int i, int j)
        {
            return "Divide of  I and J  is " + " " + ":" + (i / j);
        }

In our Form , drag and drop a button control in the Form.

First Declare a Delegate in the form :(like this)
 public delegate string matha (int i,int j);
Now paste this code in the button Click Event.

matha matadd = new matha(mathamatics.Add);
            MessageBox.Show(matadd(10, 5).ToString());
            matha matSub = new matha(mathamatics.Sub);
            MessageBox.Show(matSub(10, 5).ToString());
            matha matDivi = new matha(mathamatics.Divi);
            MessageBox.Show(matDivi(10, 5).ToString());
            matha matMul = new matha(mathamatics.Mul);
            MessageBox.Show(matMul(10, 5).ToString());



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend