locked
What is difference between delegate and interface RRS feed

  • Question

  • i know delegate is function pointer and interface and abstract is part of OOPS for implementing polymorphism.

    so why people ask often in interview what is difference between delegate and interface ?

    delegate and interface use for same purpose?

    anyone can explain what is difference between delegate and interface with sample code and example ?

    thanks

    Saturday, November 5, 2016 4:37 PM

Answers

  • why people ask often in interview what is difference between delegate and interface ?  That's a good interview question too!

    delegate and interface use for same purpose?  No.

    anyone can explain what is difference between delegate and interface with sample code and example ?

    A delegate is a type that represents references to methods with a particular parameter list and return type.

    An interface contains definitions for a group of related functionalities that a class or a struct can implement.

    Compare Delegates vs. Interfaces

    delegate int BinaryOperator( int lhs, int rhs );
    
    interface IBinaryOperator
    {
        int Perform( int lhs, int rhs );
    }
    
    class Add : IBinaryOperator
    {
        public int Perform( int lhs, int rhs )
        {
            return lhs + rhs;
        }
    }
    
    class Program
    {
        static void Main( string[] args )
        {
            IBinaryOperator obj = new Add();
            BinaryOperator op = obj.Perform;
    
            int lhs = 3;
            int rhs = 5;
            int answer_1 = obj.Perform( lhs, rhs );
            int answer_2 = op( 3, 5 );
        }
    }
    

    Saturday, November 5, 2016 5:48 PM
  • You will get better understanding here

    http://www.dotnetfunda.com/articles/show/3015/differences-between-delegates-and-interfaces-in-csharp


    MD. ROKON-UZ-ZAMAN

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Sudip_inn Monday, November 7, 2016 8:46 AM
    Saturday, November 5, 2016 5:42 PM
  • They are totally different.

    An Interface is a way to define classes, in order to force the person who creates a class to have certain things in it.

    A Delegate is a way to call a function as a variable.

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Sudip_inn Monday, November 7, 2016 8:46 AM
    Sunday, November 6, 2016 11:41 AM
  • Sorry......from the above code it is not clear the difference between delegate and interface

    For those who enjoy learning from failure, here are a few negative result experiments for you to attempt.

    • Try to derive a class from a delegate.
    • Try to declare a variable of some interface type and assigning a delegate to it.
    • Try to declare an event of type interface.
    • Try to declare a delegate that implements multiple methods.
    • Try to combine interfaces using the + operator to create a multicast interface (as you would with a multicast delegate)

    (Pro tip:  None of these experiments will succeed, because delegates and interfaces are totally different things.)

    (Pro tip #2:  Replace the words "Try to" in the above list with "You can't", if you prefer to read a list of facts.)

    • Marked as answer by Sudip_inn Monday, November 7, 2016 8:46 AM
    Monday, November 7, 2016 4:44 AM
  • Hi Mou_inn,

    Thank you for posting here.

    For your question, I provided two examples for reference.

    Interface is used to defined the same behavior and abilities for some classes(like defined what it is) generally.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace @interface
    {
        class Program
        {
            static void Main(string[] args)
            {
                Transaction t1 = new Transaction("001", "8/10/2016", 78900.00);
                Transaction t2 = new Transaction("002", "9/10/2016", 451900.00);
                t1.showTransaction();
                t2.showTransaction();
                Console.ReadKey();
            }
        }
        public interface ITransactions
        {
            // interface members
            void showTransaction();
            double getAmount();
        }
    
        public class Transaction : ITransactions
        {
            private string tCode;
            private string date;
            private double amount;
            public Transaction()
            {
                tCode = " ";
                date = " ";
                amount = 0.0;
            }
    
            public Transaction(string c, string d, double a)
            {
                tCode = c;
                date = d;
                amount = a;
            }
    
            public double getAmount()
            {
                return amount;
            }
    
            public void showTransaction()
            {
                Console.WriteLine("Transaction: {0}", tCode);
                Console.WriteLine("Date: {0}", date);
                Console.WriteLine("Amount: {0}", getAmount());
            }
        }
    }

    Delegate is used to achieve events and callback methods generally. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace @delegate
    {
        delegate int NumberChanger(int n);
        class Program
        {
            static void Main(string[] args)
            {
          
                NumberChanger nc1 = new NumberChanger(AddNum);
                NumberChanger nc2 = new NumberChanger(MultNum);
      
                nc1(25);
                Console.WriteLine("Value of Num: {0}", getNum());
                nc2(5);
                Console.WriteLine("Value of Num: {0}", getNum());
                Console.ReadKey();
            }
            static int num = 10;
            public static int AddNum(int p)
            {
                num += p;
                return num;
            }
    
            public static int MultNum(int q)
            {
                num *= q;
                return num;
            }
            public static int getNum()
            {
                return num;
            }
        }
    }

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, November 7, 2016 6:27 AM

All replies

  • You will get better understanding here

    http://www.dotnetfunda.com/articles/show/3015/differences-between-delegates-and-interfaces-in-csharp


    MD. ROKON-UZ-ZAMAN

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by Sudip_inn Monday, November 7, 2016 8:46 AM
    Saturday, November 5, 2016 5:42 PM
  • why people ask often in interview what is difference between delegate and interface ?  That's a good interview question too!

    delegate and interface use for same purpose?  No.

    anyone can explain what is difference between delegate and interface with sample code and example ?

    A delegate is a type that represents references to methods with a particular parameter list and return type.

    An interface contains definitions for a group of related functionalities that a class or a struct can implement.

    Compare Delegates vs. Interfaces

    delegate int BinaryOperator( int lhs, int rhs );
    
    interface IBinaryOperator
    {
        int Perform( int lhs, int rhs );
    }
    
    class Add : IBinaryOperator
    {
        public int Perform( int lhs, int rhs )
        {
            return lhs + rhs;
        }
    }
    
    class Program
    {
        static void Main( string[] args )
        {
            IBinaryOperator obj = new Add();
            BinaryOperator op = obj.Perform;
    
            int lhs = 3;
            int rhs = 5;
            int answer_1 = obj.Perform( lhs, rhs );
            int answer_2 = op( 3, 5 );
        }
    }
    

    Saturday, November 5, 2016 5:48 PM
  • Sorry......from the above code it is not clear the difference between delegate and interface
    Saturday, November 5, 2016 7:37 PM
  • They are totally different.

    An Interface is a way to define classes, in order to force the person who creates a class to have certain things in it.

    A Delegate is a way to call a function as a variable.

     

    Noam B.


    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

    • Marked as answer by Sudip_inn Monday, November 7, 2016 8:46 AM
    Sunday, November 6, 2016 11:41 AM
  • Sorry......from the above code it is not clear the difference between delegate and interface

    For those who enjoy learning from failure, here are a few negative result experiments for you to attempt.

    • Try to derive a class from a delegate.
    • Try to declare a variable of some interface type and assigning a delegate to it.
    • Try to declare an event of type interface.
    • Try to declare a delegate that implements multiple methods.
    • Try to combine interfaces using the + operator to create a multicast interface (as you would with a multicast delegate)

    (Pro tip:  None of these experiments will succeed, because delegates and interfaces are totally different things.)

    (Pro tip #2:  Replace the words "Try to" in the above list with "You can't", if you prefer to read a list of facts.)

    • Marked as answer by Sudip_inn Monday, November 7, 2016 8:46 AM
    Monday, November 7, 2016 4:44 AM
  • Hi Mou_inn,

    Thank you for posting here.

    For your question, I provided two examples for reference.

    Interface is used to defined the same behavior and abilities for some classes(like defined what it is) generally.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace @interface
    {
        class Program
        {
            static void Main(string[] args)
            {
                Transaction t1 = new Transaction("001", "8/10/2016", 78900.00);
                Transaction t2 = new Transaction("002", "9/10/2016", 451900.00);
                t1.showTransaction();
                t2.showTransaction();
                Console.ReadKey();
            }
        }
        public interface ITransactions
        {
            // interface members
            void showTransaction();
            double getAmount();
        }
    
        public class Transaction : ITransactions
        {
            private string tCode;
            private string date;
            private double amount;
            public Transaction()
            {
                tCode = " ";
                date = " ";
                amount = 0.0;
            }
    
            public Transaction(string c, string d, double a)
            {
                tCode = c;
                date = d;
                amount = a;
            }
    
            public double getAmount()
            {
                return amount;
            }
    
            public void showTransaction()
            {
                Console.WriteLine("Transaction: {0}", tCode);
                Console.WriteLine("Date: {0}", date);
                Console.WriteLine("Amount: {0}", getAmount());
            }
        }
    }

    Delegate is used to achieve events and callback methods generally. 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace @delegate
    {
        delegate int NumberChanger(int n);
        class Program
        {
            static void Main(string[] args)
            {
          
                NumberChanger nc1 = new NumberChanger(AddNum);
                NumberChanger nc2 = new NumberChanger(MultNum);
      
                nc1(25);
                Console.WriteLine("Value of Num: {0}", getNum());
                nc2(5);
                Console.WriteLine("Value of Num: {0}", getNum());
                Console.ReadKey();
            }
            static int num = 10;
            public static int AddNum(int p)
            {
                num += p;
                return num;
            }
    
            public static int MultNum(int q)
            {
                num *= q;
                return num;
            }
            public static int getNum()
            {
                return num;
            }
        }
    }

    I hope this would be helpful to you.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, November 7, 2016 6:27 AM