执行以下操作并返回对象或null的最佳/正确方法是什么?
如果我在try/catch子句中声明返回Dictionary对象,它可能不会返回一个对象并因此而生成编译错误.但它应该至少返回一些东西,比如null?
public static Dictionary<string,string> myFunction()
{
try {
...
Dictionary<string,string> dict = new Dictionary();
}
catch {
...
}
return dict;
}
Run Code Online (Sandbox Code Playgroud)
我只是在try/catch之外实例化返回的对象,并在调用程序中测试返回的值/长度吗?
public static Dictionary<string, string> myFunction()
{
Dictionary<string, string> dict = null;
try
{
dict = new Dictionary<string, string>();
}
catch
{
}
return dict;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1403 次 |
| 最近记录: |