Search Unity

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:
    22
    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:
    7,929
  3. Deceleris

    Deceleris

    Joined:
    Jan 3, 2018
    Posts:
    22