返回在try catch中创建的对象

Tre*_*bor 1 c# oop

执行以下操作并返回对象或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之外实例化返回的对象,并在调用程序中测试返回的值/长度吗?

Ari*_*ian 7

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)