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 Superdenny · Aug 20, 2018 at 12:12 AM · scripting problem.netcompiler errordelegates

Delegate Covariance and Contravariance using .NET 4.x Equivalent

According to this post by Microsoft the block of code below should work, but won't even compile in Unity. Does Unity not support this, despite it being introduced in C# 4.0?

 public class StartData : EventData
 {
     public float Time;
 }
 
 public class TestClass : MonoBehaviour
 {
     System.Action<EventData> _handler = null;
 
     private void Start()
     {
         _handler += TestCallback;
     }
 
     private void TestCallback(StartData startData)
     {
         Debug.Log("Testing");
     }
 }


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

2 Replies

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

Answer by Bunny83 · Aug 20, 2018 at 03:19 AM

No this doesn't work. You got the thing the other way round. The delegate has to have the more concrete parameter type while the actual handler hooked up can have a more abstract parameter type. So it would be save for the delegate to pass a "StartData" instance while the delegate receives an "EventData" instance. Since StartData is derived from EventData this works. However it doesn't work the other way round. Imagine you have another class like this:

 public class SomeOtherData : EventData
 {
     public float FooBar;
 }

Since your delegate parameter type is EventData it would be perfectly fine to pass a SomeOtherData instance to the delegate:

 _handler(new SomeOtherData());

However you can not hook up a method with a more concrete parameter type as "SomeOtherData" can't be casted to "StartData".


Contravariance applies to parameters while Covariance applies to return types. The return type acts the other way round since the data flow is in the opposite direction. So the delegate can return an abstract / base data type while the actual handler hooked up can return a derived class, but not the other way round.

Comment
Add comment · Show 2 · 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 Bunny83 · Aug 20, 2018 at 03:33 AM 0
Share

Just keep the data flow in $$anonymous$$d. Up-casting is always allowed (casting to a less derived type / base class) while implicit down casting is not allowed. So if a method expects a "Dog" you can not pass an "Animal" or anything else derived from Animal beside a Dog. However if the method expects an Animal you can pass a Dog, a Cat or anything derived from Animal. The passing along the parameter from the delegate to the hooked up method has to follow the same rules.

 Actual instance
   passed as
   parameter
       |
      \|/
    delegate
       |          // The parameters of the delegate type has to be
      \|/         // more concrete or the same as those of "method"
     method
       |          // the return type of method has to be more concrete
      \|/         // or the same as the delegate return type
   delegate
  return type
       |
      \|/
avatar image Bunny83 · Aug 20, 2018 at 03:47 AM 0
Share

Note: Don't confuse delegate co- / contravariance with generic co- / contravariance which was introduced in .NET 4. delegate co- / contravariance was supported since .NET 2.0 and array covariance since .NET 1.0 as you can read here

avatar image
0

Answer by Trevdevs · Aug 20, 2018 at 01:46 AM

Make sure this is set unity doesn't have it set to 4.x by default

alt text


capture.png (19.7 kB)
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 Superdenny · Aug 20, 2018 at 01:50 AM 0
Share

That's been set already.

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

160 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 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 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 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 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 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

Action contravarience issue, throwing ArgumentException: Incompatible Delegate Types 1 Answer

No Option to Select .NET 4.x API Compatibility 1 Answer

my Monodevelop can't code, any help or solution? 2 Answers

Unity .net framework 4.5 1 Answer

Script FPS Game Emitter Compiler error,ParticleEmitter now wont work 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