Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Generic class and method example in C#
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { PrintWhatEver("Hello world"); PrintWhatEver(2018); List<string> listString = new List<string>(); listString.Add("Hello"); listString.Add("Hellow to"); listString.Add("Hello for everyone"); PrintWhatEver(listString); List<int?> listInt = new List<int?>(); listInt.Add(1); listInt.Add(2); listInt.Add(3); PrintWhatEver(listInt); Biodata<string> biodataString = new Biodata<string>(); Biodata<int?> biodataInt = new Biodata<int?>(); Biodata<int> biodataIntNotNull = new Biodata<int>(); } private static void PrintWhatEver<T>(T thingToPrint) { if(typeof(T) == typeof(string)) { Console.WriteLine(thingToPrint as string); } else if(typeof(T) == typeof(int)) { Console.WriteLine((thingToPrint as int?).ToString()); } } private static void PrintWhatEver<T>(List<T> thingsToPrint) { if(typeof(T) == typeof(string)) { foreach(string cString in thingsToPrint as List<string>) { Console.WriteLine("\n{0}.", cString); } } else if(typeof(T) == typeof(int?)) { foreach(int? cInt in thingsToPrint as List<int?>) { Console.WriteLine("\n{0}.",cInt.ToString()); } } } } public class Biodata<T> { public Biodata() { if(typeof(T) == typeof(string)) { PrintTypeOfT(); return; } else if(typeof(T) == typeof(int?)) { PrintTypeOfT(); return; } Console.WriteLine("\ntype of {0} is not controlled in constructor.", typeof(T).ToString()); } private void PrintTypeOfT() { Console.WriteLine("\nIts a {0}.", typeof(T).ToString()); } } }
run
|
edit
|
history
|
help
0
Volatile madness
Amortizacion de prestamos con el metodo Frances
Pizzas & Array of Tuples
prime_number
sdfgyutresdf
30272 Program Ex5 if_else_odd_even
asdxasxd
Convert string to int
Practicando C#
Order of Ops 6.0 (Added Easy and Medium)