none
What is the default return type for an Event? RRS feed

  • Question

  • Hello,

    I wish like know what's the default return type for an Event because I read somebody says Event has not return type and others says Event has return type.

    What is the right opinion?

    Thanks.

    Saturday, May 18, 2013 1:02 PM

Answers

  • Events should not have a return type, because single event may have several subscribers, and return value could become ambiguous.

    Simple delegates, on the other hand, may have any return type. Just treat them as methods that are resolved at the run time.

    • Marked as answer by bit111 Sunday, May 19, 2013 10:19 AM
    Saturday, May 18, 2013 11:21 PM

All replies

  • Events are delegates with a signature that takes an object and an eventargs and returns nothing (Void/Null).  Event handlers are subroutines with the same method signature.  So the return type is Void, or in other words, there is no return type.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 18, 2013 1:21 PM
    Moderator
  • So, can I assert that Event has not return type? Or it's better to say Event returns void?

    Thanks.

    Saturday, May 18, 2013 2:23 PM
  • Events are delegates with a signature that takes an object and an eventargs and returns nothing (Void/Null).  Event handlers are subroutines with the same method signature.  So the return type is Void, or in other words, there is no return type.

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    The backing delegate for an event has no parameter restrictions - it could be any parameters or no parameter.  Also, the return type does not have to be 'void' (this is only a restriction in VB).

    e.g., the following works fine:

    public delegate int ReturnsInteger();
    public event ReturnsInteger test;
    


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Saturday, May 18, 2013 2:34 PM
  • For this reason Event can't have return type. Right?
    Saturday, May 18, 2013 2:59 PM
  • "Also, the return type does not have to be 'void' (this is only a restriction in VB)."

    Try the code in VB:

      Delegate Function ReturnInteger() As Integer
      
    Event Test As ReturnInteger

    Saturday, May 18, 2013 3:05 PM
  • Events can be declared of a delegate type which has a return type (unless you're using VB).


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Saturday, May 18, 2013 3:07 PM
  • "Also, the return type does not have to be 'void' (this is only a restriction in VB)."

    Try the code in VB:

      Delegate Function ReturnInteger() As Integer
      
    Event Test As ReturnInteger


    Right - that doesn't compile.  As I said: the requirement for a 'void' return type exists only in VB.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Saturday, May 18, 2013 3:09 PM
  • "Also, the return type does not have to be 'void' (this is only a restriction in VB)."

    Try the code in VB:

      Delegate Function ReturnInteger() As Integer
      
    Event Test As ReturnInteger


    Right - that doesn't compile.  As I said: the requirement for a 'void' return type exists only in VB.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    I meant for the OP to try it. I thought you would understand what you posted.
    Saturday, May 18, 2013 3:28 PM
  • Ok. So I can say that

    1) Event has not return type

    2) Event can be declared of a delegated which has a return type

    Saturday, May 18, 2013 4:19 PM
  • For #1, I think you can safely say that events *can* have a return type.

    Look at the following example - 'Event' is clearly returning an integer:

    	class testclass
    	{
    		public delegate int Delegate();
    		public event Delegate Event;
    
    		public testclass()
    		{
    			Event += Handler;
    			int i = Event();
    		}
    
    		internal static int Handler()
    		{
    			return 0;
    		}
    	}


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Saturday, May 18, 2013 4:34 PM
  • Events are delegates with a signature that takes an object and an eventargs and returns nothing (Void/Null).  Event handlers are subroutines with the same method signature.  So the return type is Void, or in other words, there is no return type.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    The backing delegate for an event has no parameter restrictions - it could be any parameters or no parameter.  Also, the return type does not have to be 'void' (this is only a restriction in VB).

    e.g., the following works fine:

    public delegate int ReturnsInteger();
    public event ReturnsInteger test;


    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    The backing delegate used doesn't change the recommended pattern for implementing events:

    http://msdn.microsoft.com/en-us/library/w369ty8x.aspx

    "Although events in classes that you define can be based on any valid delegate type, even delegates that return a value, it is generally recommended that you base your events on the .NET Framework pattern by using EventHandler, as shown in the following example."

    The assumption is that any event can be handled by SomeEventHandler(Object, EventArgs) without knowing anything specific about the event.  Code that breaks the pattern would also break the assumption.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 18, 2013 5:39 PM
    Moderator
  • Wasn't recommending anything against the standard. The question was whether a return type could be involved - the answer to that is 'yes'.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Saturday, May 18, 2013 7:25 PM
  • More precisely, the question is "What is the default return type for an Event".

    The answer is Void/Null/Nothing.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Saturday, May 18, 2013 8:15 PM
    Moderator
  • Events should not have a return type, because single event may have several subscribers, and return value could become ambiguous.

    Simple delegates, on the other hand, may have any return type. Just treat them as methods that are resolved at the run time.

    • Marked as answer by bit111 Sunday, May 19, 2013 10:19 AM
    Saturday, May 18, 2013 11:21 PM
  • Event must be of type Delegate, and the return type of Event would be the return type of delegate if your delegate have return type void Event will have void.

    Moreover the return value of an event would be the return value of last subscriber of that event.

    As in the following code value 6 would be printed on console as the last subscriber as Handler3.

    namespace ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                testclass cls=new testclass();
                Console.WriteLine(cls.EventReturnValue.ToString());
                Console.ReadKey();
            }
        }
    
    
        class testclass
        {
            public delegate int Delegate();
            public event Delegate Event;
    
            public int EventReturnValue
            {
                get;
                set;
            }
    
            public testclass()
            {
                Event += Handler1;
                Event += Handler2;
                Event += Handler3;
                EventReturnValue = Event();
            }
    
            internal static int Handler1()
            {
                return 0;
            }
            internal static int Handler2()
            {
                return 5;
            }
            internal static int Handler3()
            {
                return 6;
            }
        }
    }


    .

    Wednesday, May 22, 2013 6:32 AM
  • Events are basically meant for notifications which uses delegates at its core.

    Its like you notify and just return back.There cant be any return types for an Event.There is no way that you get the return type from an event.It does not fit.
    Sunday, May 26, 2013 11:48 AM