Suppose I have two functions which look like this:
public static void myFunction1(int a, int b, int c, string d)
{
//dostuff
someoneelsesfunction(c,d);
//dostuff2
}
public static void myFunction2(int a, int b, int c, Stream d)
{
//dostuff
someoneelsesfunction(c,d);
//dostuff2
}
What would be a good way to avoid repeated dostuff?
Ideas I’ve thought of, but don’t like:
- I could make d an object and cast at runtype based on type, but this strikes me as not being ideal; it removes a type check which was previously happening at compile time.
- I could also write a private helper class that takes an object and write both signatures as public functions.
- I could replace dostuff and dostuff2 with delegates or function calls or something.
I might do something like this:
… and change the name of both functions to
MyFunction(to take advantage of overloading, which is the same thingsomeoneelsesfunction()is doing.NOTE: this solution would be impractical if the string contained in the Stream is ginormous. If so, you might want to do this the other way around: read the string
dinto a stream and call the override with the stream parameter.