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. Dismiss Notice

Resolved Auto cast generic class to his parameter T

Discussion in 'Scripting' started by Deceleris, Sep 28, 2022.

  1. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    21
    Hello everyone :D

    I was wondering if it was possible to have a class like this :

    Code (CSharp):
    1. public class MyGeneric<T>
    2. {
    3.     private T value;
    4.     public T Value => value;
    5. }
    And use it like this :

    Code (CSharp):
    1. private MyGeneric<float> myValueA;
    2. private float myValueB;
    3.  
    4. void DoSomething()
    5. {
    6.     myValueA += myValueB;
    7. }
    But without having to handle all custom operators
    Literraly considering MyGeneric<T> as T everytime then assign the operations's result in the value field :)

    It would be a life saver for me, thanks in advance !
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,862
  3. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    21