Tuple

Simple Use of Tuple

You can easily return more than one value from method without using out or refparameters.

 public Tuple<int, int> SplitPoints(string point)
 {
    string[] pointList = point.Split(',');

    int x = Convert.ToInt32(pointList[0]);
    int y = Convert.ToInt32(pointList[1]);
    return Tuple.Create<int, int>(x, y);
  }

SplitPoints method split the points and returns and points in tuple.

 Tuple<int,int> points = SplitPoints("12,14");           
 string msg = string.Format("X: {0}, Y: {1}", points.Item1, points.Item2);
 MessageBox.Show(msg);

You can get the returned values from exposed properties Item1Item2, etc. by Tupleobject. Item properties of Tuple object are readonly. You can not change the value of the property.

posted @ 2013-05-10 23:31  技术商业梦  阅读(265)  评论(0编辑  收藏  举报