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;
}
}
}
2 Answers 2
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);
}
}
}
-
\$\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– Matt Rohde2012年10月26日 21:53:00 +00:00Commented Oct 26, 2012 at 21:53 -
\$\begingroup\$
selection = TryThis<int>(ReadInt); // This code worked
\$\endgroup\$Matt Rohde– Matt Rohde2012年10月26日 22:02:45 +00:00Commented Oct 26, 2012 at 22:02
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);
}
}
-
\$\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– Matt Rohde2012年10月26日 17:44:37 +00:00Commented 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\$Guffa– Guffa2012年10月26日 17:46:55 +00:00Commented Oct 26, 2012 at 17:46
Nullable<string>
.string
is a reference type. \$\endgroup\$string
is a ref type soNullable
doesn't make any sense here. \$\endgroup\$