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;
 }
 }

}

asked Oct 26, 2012 at 17:10
\$\endgroup\$
5
  • 6
    \$\begingroup\$ fyi, theres no such thing as a Nullable<string>. string is a reference type. \$\endgroup\$ Commented Oct 26, 2012 at 17:12
  • \$\begingroup\$ string is a ref type so Nullable doesn't make any sense here. \$\endgroup\$ Commented Oct 26, 2012 at 17:13
  • 2
    \$\begingroup\$ Turning exceptions into nulls is almost never a good idea. \$\endgroup\$ Commented Oct 26, 2012 at 17:28
  • \$\begingroup\$ @codesparkle Why do you say that? \$\endgroup\$ Commented Oct 26, 2012 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\$ Commented Oct 26, 2012 at 17:36

2 Answers 2

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);
 }
 }
}
answered Oct 26, 2012 at 17:33
\$\endgroup\$
2
  • \$\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\$ Commented Oct 26, 2012 at 21:53
  • \$\begingroup\$ selection = TryThis<int>(ReadInt); // This code worked \$\endgroup\$ Commented Oct 26, 2012 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);
 }
}
answered Oct 26, 2012 at 17:15
\$\endgroup\$
2
  • \$\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\$ Commented Oct 26, 2012 at 17:44
  • 1
    \$\begingroup\$ @ArmorCode: Yes, from C# 4 it can infer the type from what you return from the delegate. \$\endgroup\$ Commented Oct 26, 2012 at 17:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.