-1

Is it possible to make the DoSomething methods a single generic method?

https://dotnetfiddle.net/PXAiUV

I was thinking something like:

//I was thinking
//DoSomethingAny(c1.Method1, "an_id");

Here's the code

using System;

public class Program
{
    static MyClass c1;
    public static void Main()
    {
        c1 = new MyClass();
        Console.WriteLine("Hello World");
        DoSomething1("Test1");      
        DoSomething2("Test2");
        DoSomething3("Test3");

        //I was thinking
        //DoSomethingAny(c1.Method1, "an_id");
    }

    static int DoSomething1(string id)
    {
        //much more code above, but identical in all methods
        var x = c1.Method1(id);
        //much more code below, but identical in all methods
        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;
    }
}

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

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

    public int Method3(string id)
    {
        Console.WriteLine("Method 3 Do Work");
        return 1;
    }
}
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/221704/discussion-on-question-by-rod-how-do-i-make-the-dosomething-methods-one-gener). – Machavity Sep 18 '20 at 18:26

1 Answers1

3

Indeed, you can refactor using a delegate as parameter, thus you can write the DoSomething method only once:

public static void Main()
{
  c1 = new MyClass();
  Console.WriteLine("Hello World");
  DoSomething("Test1", c1.Method1);
  DoSomething("Test2", c1.Method2);
  DoSomething("Test3", c1.Method3);
}

static int DoSomething(string id, Func<string, int> action)
{
  //much more code above, but identical in all methods
  int x = action(id);
  //much more code below, but identical in all methods
  return x;
}

This requires that all MethodXhave the same signature else you need to create as many DoSomething as many variations, or use a generic.

static T DoSomething<T>(string id, Func<string, T> action)
{
  //much more code above, but identical in all methods
  var x = action(id);
  //much more code below, but identical in all methods
  return x;
}
  • Why waste time and clutter up Stack Overflow with this answer, when the question is so clearly [a duplicate](https://stackoverflow.com/questions/2082615/pass-method-as-parameter-using-c-sharp) of many existing questions already? – Peter Duniho Sep 18 '20 at 02:57
  • 3
    To provide help and to personalize... to personalize, my friend. And here I din't know if there is a relevant and adapted duplicate for this particular question. I had not searched and I did not know what to search... –  Sep 18 '20 at 02:59