1public class MyClass<T>2{3 public MyClass(T arg)4{5...6}78 public voidMyMethod(T arg)9{10...11}12}1314class MainClass
15{16staticvoidMain(string[] args)17{18 MyClass<string> a = new MyClass<string>("hoge");19 a.MyMethod("aaa");20}21}
1public class FixedLengthList<T>: IList<T>2{3 private readonly List<T> _list;4 private readonly int _size;5 public FixedLengthList(int size)6{7 _list = new T[size].ToList();8 this._size = size;9}1011 public T this[int index]12{13 get => _list[index];14 set => _list[index]= value;15}1617 public int Count => _list.Count;1819 public bool IsReadOnly =>((IList<T>)_list).IsReadOnly;2021 public voidAdd(T element)22{23if(_list.Count()>= _size)24{25 _list.RemoveAt(0);26}27 _list.Add(element);28}2930 public voidClear()31{32 _list.Clear();33}3435 public bool Contains(T item)36{37return _list.Contains(item);38}3940 public voidCopyTo(T[] array,int arrayIndex)41{42 _list.CopyTo(array, arrayIndex);43}4445 public IEnumerator<T>GetEnumerator()46{47return((IList<T>)_list).GetEnumerator();48}4950 public intIndexOf(T item)51{52return _list.IndexOf(item);53}5455 public voidInsert(int index, T item)56{57 _list.Insert(index, item);58}5960 public bool Remove(T item)61{62return _list.Remove(item);63}6465 public voidRemoveAt(int index)66{67 _list.RemoveAt(index);68}6970 IEnumerator IEnumerable.GetEnumerator()71{72return((IList<T>)_list).GetEnumerator();73}74}
1public class A
2{3 private constint ListSize =5;4 private FixedLengthList<Vetctor3> _vectors = new FixedLengthList<Vetctor3>(ListSize);5 private FixedLengthList<Quaternion> _quaternions = new FixedLengthList<Quaternion>(ListSize);6 private FixedLengthList<float> _floatValues = new FixedLengthList<float>(ListSize);78 public voidMethod(){9//TODO: _vectors,_quaternions,_floatValues を参照して何かを処理する;10}1112 public AddVector3(Vector3 v){13 _vectors.Add(v);14}1516 public AddQuaternion(Quaternion q){17 _quaternions.Add(q);18}19}
1class A
2{3 private int mytype;45 public A()6{7 mytype =0;8// 初期化A9}10 public A(string value)11{12 mytype =1;13// 初期化B14}15 public A(float value)16{17 mytype =2;18// 初期化C19}2021 public voidMethod()22{23if(mytype !=0)24{25 throw new ArgumentException("型が違います");26}27// 処理A28}2930 public voidMethod(string value)31{32if(mytype !=1)33{34 throw new ArgumentException("型が違います");35}36// 処理B37}3839 public voidMethod(float value)40{41if(mytype !=2)42{43 throw new ArgumentException("型が違います");44}45// 処理C46}47}48