Instantiating a delegate

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter

    Instantiating a delegate

    Hi

    in some example code I have seen delegates instantiated in the
    following fashion:

    public delegate string UserName(int id);


    UserHelper helper = new UserHelper();

    UserName un = new UserName(helper .GetUsername);


    But I have found that the following also seems to work:

    UserName un = helper.GetUsern ame;


    Is this correct?



    Thanks,
    Peter

  • Jeroen Mostert

    #2
    Re: Instantiating a delegate

    Peter wrote:
    in some example code I have seen delegates instantiated in the
    following fashion:
    >
    public delegate string UserName(int id);
    >
    >
    UserHelper helper = new UserHelper();
    >
    UserName un = new UserName(helper .GetUsername);
    >
    >
    But I have found that the following also seems to work:
    >
    UserName un = helper.GetUsern ame;
    >
    >
    Is this correct?
    >
    Yes, C# 2.0 made the compiler capable of inferring and automagically
    instantiating the appropriate delegate type. Many people (and even Visual
    Studio) still use the syntax from 1.0, but it's redundant.

    --
    J.

    Comment

    • Nicholas Paldino [.NET/C# MVP]

      #3
      Re: Instantiating a delegate

      Peter,

      From C# 2.0 and on it works. The compiler will basically expand the
      delegate instantiation out for you. It's a shorthand that I find very
      convenient.

      - Nick

      "Peter" <xdzgor@hotmail .comwrote in message
      news:OEz2O7pQJH A.764@TK2MSFTNG P05.phx.gbl...
      Hi
      >
      in some example code I have seen delegates instantiated in the
      following fashion:
      >
      public delegate string UserName(int id);
      >
      >
      UserHelper helper = new UserHelper();
      >
      UserName un = new UserName(helper .GetUsername);
      >
      >
      But I have found that the following also seems to work:
      >
      UserName un = helper.GetUsern ame;
      >
      >
      Is this correct?
      >
      >
      >
      Thanks,
      Peter
      >

      Comment

      • Peter Morris

        #4
        Re: Instantiating a delegate

        I wish the automatic code completion (when you hit tab) would use the new
        syntax. I really dislike the way it uses the old syntax!



        --
        Pete
        ====



        Comment

        • Peter

          #5
          Re: Instantiating a delegate

          Peter wrote:
          in some example code I have seen delegates instantiated in the
          following fashion:
          >
          public delegate string UserName(int id);
          >
          >
          UserHelper helper = new UserHelper();
          >
          UserName un = new UserName(helper .GetUsername);
          >
          >
          But I have found that the following also seems to work:
          >
          UserName un = helper.GetUsern ame;

          Thanks for the answers. In my naivety I had used the form:
          UserName un = helper.GetUsern ame
          without thinking about it - to me it seems natural.
          It was only later I saw the "real" syntax.

          /Peter


          Comment

          Working...