Last active
April 10, 2016 10:19
-
-
Save tomaszkacmajor/f685238a3f0ec57355d8e9a78c0fc032 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace GenericsTests1 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
ExampleClass example = new ExampleClass(); | |
example.RunAll(); | |
Console.ReadKey(); | |
} | |
} | |
public class ExampleClass | |
{ | |
string s = "Score"; | |
string result = ""; | |
string[] names = new string[5] { "Score 1", "Score 2", "Score 3", "Score 4", "Score 5" }; | |
string[] results = new string[5]; | |
int[] integers = new int[5] { 2, 4, 5, 1, 3 }; | |
double[] doubles = new double[5] { 2.3, 4.5, 5.4, 1.3, 3.1 }; | |
public void RunAll() | |
{ | |
AddIntScore_Test(); | |
AddIntScore_Extension_Test(); | |
AddGenericScore_Test(); | |
AddGenericScore_Test2(); | |
AddGenericScore_Func_Test(); | |
AddGenericScore_Func_Test2(); | |
AddGenericScore_LambdaFunc_Test(); | |
} | |
public void AddIntScore_Test() | |
{ | |
Console.WriteLine("AddIntScore_Test"); | |
result = Utilities.AddIntScore(s, 5); | |
Console.WriteLine(result); | |
Console.WriteLine(); | |
} | |
public void AddIntScore_Extension_Test() | |
{ | |
Console.WriteLine("AddIntScore_Test"); | |
result = s.AddIntScore_Extension(5); | |
Console.WriteLine(result); | |
Console.WriteLine(); | |
} | |
public void AddGenericScore_Test() | |
{ | |
Console.WriteLine("AddIntScore_Test"); | |
result = s.AddGenericScore(5); | |
Console.WriteLine(result); | |
Console.WriteLine(); | |
} | |
public void AddGenericScore_Test2() | |
{ | |
Console.WriteLine("AddGenericScore_Test2"); | |
result = s.AddGenericScore(5.6); | |
Console.WriteLine(result); | |
Console.WriteLine(); | |
} | |
public void AddGenericScore_Func_Test() | |
{ | |
Console.WriteLine("AddGenericScore_Func_Test"); | |
results = names.AddGenericScore(AddFunc); | |
foreach (var item in results) | |
{ | |
Console.WriteLine(item); | |
} | |
Console.WriteLine(); | |
} | |
public void AddGenericScore_Func_Test2() | |
{ | |
Console.WriteLine("AddGenericScore_Func_Test2"); | |
results = doubles.AddGenericScore(AddFunc2); | |
foreach (var item in results) | |
{ | |
Console.WriteLine(item); | |
} | |
Console.WriteLine(); | |
} | |
public void AddGenericScore_LambdaFunc_Test() | |
{ | |
Console.WriteLine("AddGenericScore_LambdaFunc_Test"); | |
results = names.AddGenericScore((x, i) => x + ": " + integers[i]); | |
foreach (var item in results) | |
{ | |
Console.WriteLine(item); | |
} | |
Console.WriteLine(); | |
} | |
string AddFunc(string str, int i) | |
{ | |
return str + ": " + integers[i].ToString(); | |
} | |
string AddFunc2(double num, int i) | |
{ | |
return (num * integers[i]).ToString(); | |
} | |
} | |
public static class Utilities | |
{ | |
public static string AddIntScore(string str, int parameter) | |
{ | |
return str + ": " + parameter.ToString(); | |
} | |
public static string AddIntScore_Extension(this string str, int parameter) | |
{ | |
return str + ": " + parameter.ToString(); | |
} | |
public static string AddGenericScore<T>(this string str, T parameter) | |
{ | |
return str + ": " + parameter.ToString(); | |
} | |
public static TResult[] AddGenericScore<TInput, TResult>(this TInput[] input, Func<TInput, int, TResult> func) | |
{ | |
TResult[] result = new TResult[input.Length]; | |
for (int i = 0; i < input.Length; i++) | |
result[i] = func(input[i], i); | |
return result; | |
} | |
public static TResult[] Apply<TInput, TResult>(this TInput[] vector, Func<TInput, int, TResult> func) | |
{ | |
TResult[] result = new TResult[vector.Length]; | |
for (int i = 0; i < vector.Length; i++) | |
result[i] = func(vector[i], i); | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment