Link to home
Start Free TrialLog in
Avatar of jasoncpp
jasoncpp

asked on

Simple delegates question

Hi, I am trying to understand delegates a little better as a c# beginner.  Why does the call to getDel not cause execution of both getMe and getX.  I do not think the abstract class has any relevance to this question, I just do not know why both functions - getX and getMe - are not executed as they are both registered.  Only getX is executed.

Thanks for explaining more about delegates - but please do not post a bare link to a lot of text on delegates, I just want it explained on here concisely ;)

using System;

namespace Abstract1
{
      /// <summary>
      /// Summary description for Class1.
      /// </summary>
      class Class1
      {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main(string[] args)
            {
                  Person p = new Person();
                  p.getDel();
            }
      }
}


using System;

namespace Abstract1
{
      /// <summary>
      /// Summary description for Being.
      /// </summary>
      ///
      public delegate int NDelegate();

      public abstract class Being
      {
            private NDelegate adel;

            public Being()
            {
                  adel = new NDelegate(getMe);
                  adel += new NDelegate(getX);
            }

            public void getDel()
            {
                  Console.WriteLine("{0}", adel());
            }

            public abstract int getMe();
            public abstract int getX();
            public abstract int setMe(int i);

            public int anotherMe()
            {
                  return 50;
            }
      }

      public class Person : Being
      {
            public override int getMe()
            {
                  return 55;
            }

            public override int getX()
            {
                  return 56;
            }

            public override int setMe(int i)
            {
                  return i * 3;
            }


      }
}
ASKER CERTIFIED SOLUTION
Avatar of eternal_21
eternal_21

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial