OK, the whole new approach. Since you have several possible types and you want to use the "joker" method, you can save the values ​​as a collection of keys and values ​​in your class, then such a method will become possible.
First, to keep the values ​​inside:
public class TestClass { private Dictionary<Type, Array> _values = new Dictionary<Type, Array>(); }
Now, to populate this collection with actual data:
_values.Add(typeof(int?), new int[] { 1, 2, 3 }); _values.Add(typeof(string), new string[] { "a", "b", "c", "d", "e" });
And finally, the joker method:
public T Get<T>(int index) { Type type = typeof(T); Array array; if (_values.TryGetValue(type, out array)) { if (index >= 0 && index < array.Length) { return (T)array.GetValue(index); } } return default(T); }
Using:
for (int i = 0; i < 10; i++) { int? id = testClass.Get<int?>(i); string name = testClass.Get<string>(i);
source share