Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Does Covariance & Contravariance work in Unity?

Discussion in 'Scripting' started by Deleted User, Mar 8, 2015.

  1. Deleted User

    Deleted User

    Guest

    I have an interface:

    Code (CSharp):
    1. public interface IScrollable<out T> where T : MonoBehaviour
    2. {
    3.         IEnumerable<T> ScrollableItems { get; }
    4. }
    But I'm getting the error: "The covariant type parameter `T' must be invariantly valid on `IScrollable<T>.ScrollableItems'"
     
  2. Limyc

    Limyc

    Joined:
    Jan 27, 2013
    Posts:
    29
    Unfortunately, generic covariance/contravariance was introduced in .Net 4.0. Unity is using a Mono version that equates to .Net 3.5. So until Unity can update Mono, the code listed above will not work.
     
    Deleted User likes this.