Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 返回整数和字符串_C#_.net - Fatal编程技术网

C# 返回整数和字符串

C# 返回整数和字符串,c#,.net,C#,.net,我有一个函数如下。我需要返回两个参数。首先是作为列表的索引,它由函数完成。我需要返回的另一个参数是字符串str。 对于这些输出,您的最佳建议是什么?有两个不同参数的列表?还是怎样请让我知道你的想法!谢谢 public List<int> index_selexted(TreeNodeCollection treeView, List<int> list) { List<int, List<string>> output_typ = new

我有一个函数如下。我需要返回两个参数。首先是作为
列表的索引,它由函数完成。我需要返回的另一个参数是字符串
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;
}
public List index\u selexted(TreeNodeCollection树视图,List List)
{
列表输出类型=新列表();
foreach(treeView中的TreeNode节点)
{
如果(节点已选中)
{
添加(节点索引);
字符串str=Regex.Match(node.Text,@“\(.*?)\”))。组[1]。值;
}
其他的
{
索引_selexted(node.Nodes,list);
}
}
退货清单;
}
您可以选择

public class IndexSelectionResult{
    public List<something> Index{get;set;}  
    public String StringResult 
}
公共类索引选择结果{
公共列表索引{get;set;}
公共字符串字符串结果
}
然后返回一个实例,或者,如果您懒惰,可以返回一个元组:

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

好吧,既然
TreeNode.Index
在整个
TreeNodeCollection
中不是唯一的,那么
Dictionary
不是一个选择,但是
Dictionary
就可以了

//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;
}
我添加了
dict==null
,以简化调用:

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

我相信你想要这个:

    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;
    }

而不是在示例中使用列表(list > OutPuthTyp=新List.);考虑< /P> 使用字典:

var output_typ = new Dictionary<int, List<string>>();
var output_typ=new Dictionary();
或元组列表:

var output_typ = new List<Tuple<int, List<string>>();
var输出类型=新列表使用

var res=new Tuple(“string1”,new List());

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

使用类

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"
   }
}
classmyresult{
公共列表MyList{get;set;}
公共字符串MyString{get;set;}
}
已选择公共MyResult索引(arg1…,arg2…){
返回新的MyResult{
MyList=输出列表,
MyString=“outputString”
}
}
使用类是我的首选方式。虽然它可能会混乱,如果你有许多返回类型,它是迄今为止最可读的解决方案

使用元组

public Tuple<List<int>, string> index_selected(arg1..., arg2...) {
   return Tuple.Create(outputList, "outputString");
}
已选择公共元组索引(arg1…,arg2…){
返回Tuple.Create(outputList,“outputString”);
}
我的第二个转到选项是元组。确定元组中包含的值代表什么要困难得多。但是不需要创建更多的类,这是一个快速的解决方案(我主要用于可读性不太重要的私有方法)

使用out关键字

public List<int> index_selected(arg1..., out string resultString) {
   resultString = null;
   /* Doing calculations and such */
   resultString = "
   return outputList;
}
已选择公共列表索引(arg1…,输出字符串resultString){
resultString=null;
/*做计算之类的*/
结果字符串=”
返回输出列表;
}
在这种情况下,传递给resultString参数的字符串将被您在方法中分配给它的任何内容所替换(请参阅)。根据您的用例,您可能还需要查看


这种方法很容易出错,通常不受欢迎。

恐怕我很难理解你的意思。你能试着澄清一下你的问题吗?一个简短但完整的程序,用示例输入和输出来演示这个问题,会很有帮助。请注意,这看起来并不是WinForms特有的-你碰巧在使用<代码> TeeRooDeCopys<代码>,但是任何树类型都可能允许你显示相同的问题。你是否考虑过使用“<代码>外< /Cord>关键字”的参数?它们可以帮助你从函数中得到两个(和更多)“返回”。<代码>列表OutPuthTyp=新列表();
无法编译。谢谢您的回答,但请注意,一个输出是整数,另一个是字符串。
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");
}
public List<int> index_selected(arg1..., out string resultString) {
   resultString = null;
   /* Doing calculations and such */
   resultString = "
   return outputList;
}