返回整数和字符串

本文关键字:字符串 整数 返回 | 更新日期: 2023-09-27 18:37:19

我有一个如下函数。我需要返回两个参数。首先是list的索引,它由函数完成。我需要返回的另一个参数是一个字符串str。对于这些输出,您最好的建议是什么?具有两个不同参数的列表?还是什么?请让我知道你的想法!谢谢

public List<int> index_selexted(TreeNodeCollection treeView, List<int> list)
{
    List<int, List<string>> output_typ = new List<int, >();
    foreach (TreeNode node in treeView)
    {
        if (node.Checked)
        {
            list.Add(node.Index);
            string str = Regex.Match(node.Text, @" '((.*?)') ").Groups[1].Value;            
        }
        else
        {
            index_selexted(node.Nodes, list);
        }
    }
    return list;
}

返回整数和字符串

好吧,由于TreeNode.Index在整个TreeNodeCollection中不是唯一的,因此Dictionary<int, String>不是一种选择,但Dictionary<int, List<String>>可以

//TODO: find a better name for dict
public Dictionary<int, List<String>> index_selexted(
  TreeNodeCollection treeView, 
  Dictionary<int, List<String>> dict == null) { // == null for autocreation
  if (null == treeView)  
    throw new ArgumentNullException("treeView");
  if (null == dict)
    dict = new Dictionary<int, List<String>>();
  foreach (TreeNode node in treeView) 
    if (node.Checked) {
      String match = Regex.Match(node.Text, @" '((.*?)') ").Groups[1].Value; 
      List<String> list;
      if (dict.TryGetValue(node.Index, out list))  
        list.Add(match);
      else
        dict.Add(node.Index, new List<String>() {match}); 
    }
    else 
      index_selexted(node.Nodes, dict);
  return dict;
}

所以你会有这样的东西作为输出:index + 它的所有匹配项:

  {1, ["abc", "def", "gh"]}
  {3, ["xyz"]}

我添加了dict == null以使通话更容易:

  // You don't have to pre-create the dictionary
  var myDict = index_selexted(myTreeView.Nodes);

使用元组

var res = new Tuple<string, List<string>>("string1", new List<string>());

你可以做

public class IndexSelectionResult{
    public List<something> Index{get;set;}  
    public String StringResult 
}
并返回一个

实例,或者,如果你懒惰,你可以返回一个元组:

public Tuple<List<string>, string>> myFunction(){ /* code */}

我相信你想要这个:

    public static List<int> index_selexted(TreeNodeCollection treeView, out string str)
    {
        str = null;
        var list = new List<int>();
        var output_typ = new List<int>();
        foreach (TreeNode node in treeView)
        {
            if (node.Checked)
            {
                list.Add(node.Index);
                str = Regex.Match(node.Text, @" '((.*?)') ").Groups[1].Value;            
            }
            else
            {
                index_selexted(node.Nodes, list);
            }
        }
        return list;
    }

用法如下:

var treeview = sometreeview;
string output;
var result = index_selected(treeview, out output);
Console.WriteLine(output);

而不是在示例中使用列表 (List> output_typ = new List();)考虑

使用字典:

var output_typ = new Dictionary<int, List<string>>();

或元组列表:

var output_typ = new List<Tuple<int, List<string>>();

希望这有帮助

实际上,有很多方法可以做到这一点,这取决于您的特定用例,您更喜欢哪一种。

使用类

class MyResult {
    public List<int> MyList { get; set; }
    public string MyString { get; set; }
}
public MyResult index_selected(arg1..., arg2...) {
   return new MyResult {
      MyList = outputList,
      MyString = "outputString"
   }
}

使用类是我的首选方式。尽管如果您有许多返回类型,它可能会混乱,但它是迄今为止最具可读性的解决方案。

使用元组

public Tuple<List<int>, string> index_selected(arg1..., arg2...) {
   return Tuple.Create(outputList, "outputString");
}

我的第二个goto选项是一个元组。确定元组中包含的值表示什么要困难得多。但是不需要创建更多的类,并且是一个快速的解决方案(我主要将其用于可读性不是一个太大问题的私有方法)。

使用 out 关键字

public List<int> index_selected(arg1..., out string resultString) {
   resultString = null;
   /* Doing calculations and such */
   resultString = "
   return outputList;
}

在这种情况下,传递给 resultString 参数的字符串将被您在方法中分配给它的任何内容替换(请参阅 out 关键字)。根据您的用例,您可能还需要查看 ref 关键字。

这种方法很容易出错,通常不适用。