Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Inok · Oct 02, 2015 at 10:30 PM · c#event

Concrete Delegate vs EventHandler for Events

For events i can do 2 ways:

1) public delegate void Event_Handler ();
public event Event_Handler Some_Event;

2) public event EventHandler Some_Event;

I know that 2) variant better because no need to define concrete delegate "public delegate void Event_Handler ();" But it not better because i need to write in all methods - subscribers parameters:

void Method_For_Event (object sender, EventArgs event_args)

So what variant you think better and why?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Bunny83 · Oct 03, 2015 at 06:24 AM

Well, as always it depends on what you need. The EventHandler type is just a predefined general purpose delegate type that is commonly used by most GUI controls of the .NET framework. As most events have some object that is responsible for raising the event (the "sender") and often an event requires some parameters. The EventHandler type is just a generic way to maintain compatibility with other systems.

Your line:

 public delegate void Event_Handler ();

Just declares a new delegate type with no parameters and no return type. You would only need to declare that delegate once for your entire project.

However you don't need to declare that delegate at all, since there is already the System.Action delegate type which is exactly the same as your type. There are also generic versions of that type where you can easily specify parameter types.

For example Action<Vector3, float> represents a delegate like this:

 public delegate void DelegateTypeName (Vector3 v, float f);

So if you just want to declare an event with no parameters you can simply do:

 public event System.Action Some_Event;

Furthermore there's also the "Func" type which represents a delegate with a return type.

 Func<int, int, string>

will represent

 public delegate string DelegateTypeName (int i1, int i2);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Inok · Oct 03, 2015 at 02:41 PM 0
Share

Looks like use "Action" and "Func" ins$$anonymous$$d of "delegate" in most cases more comfortable.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Relationship between Events and Update() 2 Answers

Adding single Event listener to multiple ui buttons via scripting 1 Answer

Serialize UnityEvent for use in EditorWindow 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges