MakeMeGeneric

CSharp · Console

/Program.cs

using System;

public class Program
{
	static MyClass c1;
	public static void Main()
	{
		c1 = new MyClass();
		DoSomething1("Test1");
		DoSomething2("Test2");
		DoSomething3("Test3");
		DoSomething(c1.Method1, "Test4"); //Generic way Answer came from Aluan https://dotnetfiddle.net/7CQBLL
		DoSomethingX(() => c1.Method2("HelloWorld"));
		//Rework https://dotnetfiddle.net/DHVSq7
	}

	static int DoSomething1(string id)
	{
		var x = c1.Method1(id);
		return x;
	}

	static int DoSomething2(string id)
	{
		var x = c1.Method2(id);
		return x;
	}

	static int DoSomething3(string id)
	{
		var x = c1.Method3(id);
		return x;
	}

	static T DoSomething<T>(Func<string, T> f, string id)
	{
		var x = f(id);
		return x;
	}

	static T DoSomethingX<T>(Func<T> f)
	{
		return f();
	}
}

public class MyClass
{
	public int Method1(string id)
	{
		Console.WriteLine("Method 1 Do Work - {0}", id);
		return 1;
	}

	public int Method2(string id)
	{
		Console.WriteLine("Method 2 Do Work - {0}", id);
		return 1;
	}

	public int Method3(string id)
	{
		Console.WriteLine("Method 3 Do Work - {0}", id);
		return 1;
	}
}
;