请问如何在自定义函数中返回两个值,一个是int型,一个是 List<string>型。C#是否有结构体,能否通过结构体实现,请给出示范代码,谢谢

解决方案 »

  1.   


    class YourClass
    {
    int Num;
    List<string> Items
    }
    private YourClass Foo()
    {
    YourClass yc = new YourClass();
    yc.Num = 0;
    yc.Items = new List<string>();
    yc.Items.Add("abc");
    return yc;
    }
      

  2.   

    http://msdn.microsoft.com/zh-cn/library/dd268536.aspx
      

  3.   

    return list<string>
    out int
      

  4.   


    请问我这种情况使用返回类的方式好,还是使用out关键字的方式好?
      

  5.   

    int fun(out List<string> outlist)
    {
    asdfasdfasdf
    outlist=new List<string>();
    return 5;
    }
      

  6.   

    第一种:private List<T> Get(out int)
    {}
    第二种:struct Test<T>
    {
      int A {get;set;}
      List<T> List {get;set;}
    }
    private Test<T> Get()
    {}
      

  7.   

    List<string> strlist = new List<string>();
    private int Foo(ref List<string> strlist)
    {
    int result=0;
    ...........
    return result;
    }
      

  8.   

    C# 函数只有一个返回值,可以使用 out 参数达到相同的效果
      

  9.   

    你要是有很多返回值的话,可以参考楼上各路大神的思路,两个返回值为什么要搞这么复杂呢?很明显是KeyValuePair最省事啊,而且我估计你这两个值之间八成是有关联的,以Int为键很方便,这也是很常见的业务模型,我自己也经常用到这种结构。循环调用的话,还能直接就压进Dictionary,多省事。