简体   繁体   English

如果可能,如何为方法选择返回值?

[英]How to make return value optional for a method,if possible?

I have a method private static DataTable ParseTable(HtmlNode table) and sometimes this method has no return value then I want to make return value optional, is it possible ? 我有一个方法private static DataTable ParseTable(HtmlNode table) ,有时这个方法没有返回值,那么我想让返回值可选,是否可能?

I have tried with if condition.But there is error. 我试过if条件。但是有错误。

How can I make return value optional for the method if possible ? 如果可能,如何为方法选择返回值?

You can't make the return value optional. 您不能使返回值可选。

Your two options are to either return a null or an empty DataTable : 您的两个选项是返回null或空DataTable

return new DataTable();

OR: 要么:

return null;

Which one is right? 哪一个是对的? It depends on your application logic and how much null checking you are willing to do in your calling functions. 这取决于您的应用程序逻辑以及您愿意在调用函数中执行多少null检查。

Update: (following comment) 更新:(以下评论)

This is how to return conditionally (assumes a variable called dataTable): 这是有条件地返回(假设一个名为dataTable的变量):

 if(gotTable)
 {
   return dataTable;
 }
 else
 {
   return null;
 }

That's what null is for. 这就是null的用途。 Your method may return no object by returning null . 您的方法可能通过返回null返回无对象。

Keep in mind that returning null may complicate matters for the callers as they must check for null before using the returned reference, so in some cases it may be better to return a Null Object . 请记住,返回null可能会使调用者的事情变得复杂,因为在使用返回的引用之前必须检查null,因此在某些情况下返回Null对象可能更好。

您始终可以返回null以指示没有有意义的返回值。

If your method is called ParseTable and it fails "Parse" a "Table", then it should throw an exception. 如果您的方法被称为ParseTable并且它“失败”“表”失败,那么它应该抛出异常。 The advantage of this is that the exception can give the caller information about why it couldn't parse (html was invalid, unexpected column etc). 这样做的好处是异常可以给调用者提供有关它无法解析的原因的信息(html无效,意外的列等)。 The problem with returning null is that an unexpected nullreference exception almost never tells you the cause of the problem. 返回null的问题是,意外的空引用异常几乎从不告诉您问题的原因

The "right" way to make a method that tries to parse a table, but happily does nothing if no result could be found is: 制作试图解析表格的方法的“正确”方法,但如果找不到结果,则无所事事:

public bool TryParseTable(HtmlNode table, out DataTable result){
    // your code...
    if(success)
    {
        result = //the table you parsed
        return true;
    }
    else
    {
        result = null;
        return false;
    }
}

Ok, so "result" could be null after calling this method, but at least the caller is more inclined to use an if statement thanks to the return type and method name. 好的,所以调用此方法后“result”可能为null,但由于返回类型和方法名称,至少调用者更倾向于使用if语句。

private static void ParseTable(HtmlNode table, out DataTable data)
{
   // do the parse, fill bool gotTable 
   data = gotTable ? new DataTable() : null;
}

private static void ParseTable(HtmlNode table)
{
   ParseTable(table, out null);
}

if caller need table 如果来电者需要表格

DataTable data;
ParseTable(table, out data);

if not 如果不

ParseTable(table);

EDIT: I can't find how to implement optional out/ref parameters. 编辑:我找不到如何实现可选的out/ref参数。 So maybe it's impossible until .NET 4.0 or fully impossible. 因此,在.NET 4.0或完全不可能之前,这可能是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM