Search Unity

Bug Unity wants me to ruin good syntax

Discussion in 'Scripting' started by Askofep, Jan 23, 2023.

  1. Askofep

    Askofep

    Joined:
    Jan 23, 2023
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [Serializable]
    6. public class KVPairObject<K, V>
    7. {
    8.     public K Key;
    9.     public V Value;
    10.     KVPairObject<K, V>(K key, V Val)
    11.     {
    12.         Key = key;
    13.         Value = Val;
    14.     }
    15. }
    16.  
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    Care to explain?
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    It should be

    Code (CSharp):
    1. [Serializable]
    2. public class KVPairObject<K, V>
    3. {
    4.     public K Key;
    5.     public V Value;
    6.     public KVPairObject(K key, V Val)
    7.     {
    8.         Key = key;
    9.         Value = Val;
    10.     }
    11. }
    12.  
    Generic angle brackets on method would indicate a generlc method. However you're inside a generic class. So the generic parameters K and V are given / bound at class level. So inside the class K and V are just ordinary types. Note that the constructor should be public. If it's private, nobody could create an instance of that class except the class itself.
     
    arkano22 and spiney199 like this.
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    MaskedMouse, spiney199 and Bunny83 like this.
  5. Askofep

    Askofep

    Joined:
    Jan 23, 2023
    Posts:
    2
    OWO
    OK