2016-02-25 94 views
0

我试图返回正则表达式匹配的字符串数组stringArray以及相关的匹配match_countstring_indexmatch_length。如何在方法返回中发送多个不同数据类型的值。我已阅读Tuple但所有示例基本上都显示了多个值,但它们似乎总是整数而不是混合类型。我不能解决如何在我的例子中使用它来实现字符串数组和整数。方法,返回不同数据类型的多个值

string ptrn_coords = @"- Coordinates: \[ ([\-0-9]+), ([\-0-9]+), ([\-0-9]+) \]"; 

    private void button3_Click(object sender, EventArgs e) 
    { 
     string[] matches; 
     matches = GetMatches(s, ptrn_coords); 
    } 



    private static string[] GetMatches(string input, string pattern) 
    { 

     string[] stringArray; 
     Match mc = Regex.Match(input, pattern); 

     int string_index = 0; 
     int match_length = 0; 
     int match_count = 0; 
     List<String> listTemp = new List<string>(); 
     while (mc.Success) 
     { 
      match_count++; 
      string_index = mc.Index; 
      match_length = mc.Length;    
      listTemp.Add(mc.ToString()); 

      //MessageBox.Show("Match Text: " + mc.ToString() + " Index: " + string_index + " Length: " + match_length + " Count: " + match_count); // Test Message 

      mc = mc.NextMatch(); 
     } 
     stringArray = listTemp.ToArray<String>(); 
     return stringArray; 

    } 
+0

真的无法理解倒票。这似乎是一个明确的问题 –

回答

2

元组是类型安全的,可以安全地返回原始类型和复杂类型的混合。下面是一个例子:

void Main() 
{ 
    var x = GetTuple(); 
    Console.WriteLine($"{x.Item1} {x.Item2} {x.Item3.GetType().Name}"); // Prints "string 5 MyClass" 
} 

.... 

public Tuple<string, int, MyClass> GetTuple() 
{ 
    string myString = "string"; 
    int myInt = 5; 
    MyClass myClass = new MyClass(); 
    return Tuple.Create(myString,myInt,myClass); 
} 
+0

感谢您的例子。检索返回的值是它总是突破的地方。这使得它非常简洁。感谢帮助了很多。 –

+0

真棒,我很高兴它帮助。如果满足您的问题以便将来帮助其他人,请务必将其中一个答案标记为已接受。 –

2

不知道你要回来,如果你想返回的字符串数组和matchcount等,你可以简单地定义这些属性的类,然后返回这个类的一个实例是什么。