using System; public class Starter { public static void Main() { MyClass<int> obj = new MyClass<int>(); obj.MethodA(); } } class MyClass<T> { public T MethodA() { T var = default(T); Console.WriteLine(this.GetType().ToString()); return var; } }
Author: coder
A generic Point structure.
using System; using System.Collections.Generic; using System.Text; public struct Point<T> { private T xPos; private T yPos; public Point(T xVal, T yVal) { xPos = xVal; yPos = yVal; } public T X { get { return xPos; } set { xPos = value; } } public T Y { get { return yPos; } set { yPos = value; } } public override string ToString() { return string.Format("[{0}, {1}]", xPos, yPos); } public void ResetPoint() { xPos = default(T); yPos = default(T); } }
combining inheritance of generic types and constraints:
using System; using System.Collections; public class MyClass<T> where T : IComparable { } public class YClass<T> : MyClass<T> where T : IComparable { } public class XClass<T> : MyClass<T> where T : IComparable, IDisposable { } public class BClass<Y> where Y : IEnumerable { } public class AClass<Z> : BClass<int[]> where Z : IDisposable { }
Nested Types
using System; public class Starter { public static void Main() { MyClass<int>.Nested<double> obj = new MyClass<int>.Nested<double>(); obj.MethodA(10, 12.34); } } public class MyClass<T> { public void MethodA(T arg) { } public class Nested<S> { public void MethodA(T arg1, S arg2) { Console.WriteLine("arg1: {0}", arg1.GetType().ToString()); Console.WriteLine("arg2: {0}", arg2.GetType().ToString()); } } }
Call ToString on generic type
using System; using System.Collections.Generic; using System.Text; public class HelloGenerics<T> { private T _thisTalker; public T Talker { get { return this._thisTalker; } set { this._thisTalker = value; } } public void SayHello() { string helloWorld = _thisTalker.ToString(); Console.WriteLine(helloWorld); } } public class GermanSpeaker { public override string ToString() { return "GermanSpeaker!"; } } public class SpainishSpeaker { public override string ToString() { return "SpainishSpeaker"; } } public class EnglishSpeaker { public override string ToString() { return "EnglishSpeaker"; } } class Program { static void Main(string[] args) { HelloGenerics<GermanSpeaker> talker1 = new HelloGenerics<GermanSpeaker>(); talker1.Talker = new GermanSpeaker(); talker1.SayHello(); HelloGenerics<SpainishSpeaker> talker2 = new HelloGenerics<SpainishSpeaker>(); talker2.Talker = new SpainishSpeaker(); talker2.SayHello(); } }
Inherit Type Parameter
using System; using System.Collections.Generic; using System.Text; public class MyBaseClass<U> { private U _parentData; public MyBaseClass() { } public MyBaseClass(U val) { this._parentData = val; } } public class MySubClass<T, U> : MyBaseClass<U> { private T _myData; public MySubClass() { } public MySubClass(T val1, U val2) : base(val2) { this._myData = val1; } }
Output the type information about the generic parameters
using System; using System.Collections.Generic; using System.Text; public class OneParamType<T> {} public class TwoParamType<T, U> {} public class TypeDumper<T, U, V> { public static void DumpTypeInfo() { Console.WriteLine(typeof(T)); Console.WriteLine(typeof(U)); Console.WriteLine(typeof(V)); Console.WriteLine(typeof(OneParamType<String>)); Console.WriteLine(typeof(OneParamType<T>)); Console.WriteLine(typeof(TwoParamType<U, int>)); Console.WriteLine(typeof(TwoParamType<T, V>)); } public static void ShowTypeInfo() { TypeDumper<String, int, Double>.DumpTypeInfo(); } }