4
\$\begingroup\$

I'm looking for a way to simplify this code, because I could develop more overloads for TryThis I made the string and int both of class Nullable so that in each overloaded function, the catch block could return the same value.

The problem is I need, if possible, no overloads of TryThis. The function overloads are both identical, except for the type of delegate they are passed. Is there some kind of variable that would encompass any delegate that can be executed?

class Program
{
    delegate int MyIntReturn();
    delegate string MyStringReturn();
    static private MyIntReturn ReadInt = () => {return int.Parse(Console.ReadLine()); };
    static private MyStringReturn ReadString = () => { return Console.ReadLine(); };

    static private Nullable<int> TryThis(MyIntReturn MyAction)
    {
        try
        {
            return MyAction();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return null;
        }
    }
    private static Nullable<string> TryThis(MyStringReturn MyAction)
    {
        try
        {
            return MyAction();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return null;
        }
    }

}

\$\endgroup\$

migrated from stackoverflow.com Oct 26 '12 at 17:14

This question came from our site for professional and enthusiast programmers.

  • 6
    \$\begingroup\$ fyi, theres no such thing as a Nullable<string>. string is a reference type. \$\endgroup\$ – Daniel A. White Oct 26 '12 at 17:12
  • \$\begingroup\$ string is a ref type so Nullable doesn't make any sense here. \$\endgroup\$ – JonH Oct 26 '12 at 17:13
  • 2
    \$\begingroup\$ Turning exceptions into nulls is almost never a good idea. \$\endgroup\$ – Adam Oct 26 '12 at 17:28
  • \$\begingroup\$ @codesparkle Why do you say that? \$\endgroup\$ – Matt Rohde Oct 26 '12 at 17:31
  • 1
    \$\begingroup\$ Because exceptions need to be handled, not hidden, in nearly every case. If an exception happens, it tells you what went wrong. If null is returned, you have no idea what went wrong. \$\endgroup\$ – Adam Oct 26 '12 at 17:36
4
\$\begingroup\$

Generics and Delegates. Note that you can't return null in this modified version of TryThis, so we use the default(T) method to return whatever's most sensible.

class Program
{
    private delegate T TypeReturn<T>();
    static private TypeReturn<int> ReadInt = () => int.Parse(Console.ReadLine());
    static private TypeReturn<string> ReadString = () => Console.ReadLine(); 
    static private T TryThis<T>(TypeReturn<T> MyAction )
    {
        try
        {
            return  MyAction() ;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return default (T);
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I am trying to call the code you produced with selection = TryThis(ReadInt()); // selection is defined as int earlier in code and i get the error: The type arguments for method 'ConsoleApplication1.Program.TryThis<T>(ConsoleApplication1.Program.DelTypeReturn<T>)' cannot be inferred from the usage. Try specifying the type arguments explicitly. I thought that C# can infer that it is of type int... \$\endgroup\$ – Matt Rohde Oct 26 '12 at 21:53
  • \$\begingroup\$ selection = TryThis<int>(ReadInt); // This code worked \$\endgroup\$ – Matt Rohde Oct 26 '12 at 22:02
5
\$\begingroup\$

You can use the generic delegate Func<>:

static private T TryThis<T>(Func<T> MyAction) {
    try {
        return MyAction();
    } catch (Exception ex) {
        Console.WriteLine(ex.ToString());
        return default(T);
    }
}
\$\endgroup\$
  • \$\begingroup\$ If I'm understanding this correctly, is T inferred to be a return type because it is put in angle brackets after TryThis? If so, I didn't realize C# could infer the the return type like that. \$\endgroup\$ – Matt Rohde Oct 26 '12 at 17:44
  • 1
    \$\begingroup\$ @ArmorCode: Yes, from C# 4 it can infer the type from what you return from the delegate. \$\endgroup\$ – Guffa Oct 26 '12 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.