组元是C# 4.0引入的一个新特性,编写的时候需要基于.NET Framework 4.0或者更高版本。组元使用泛型来简化一个类的定义。

先以下面的一段代码为例子:

public class Point
{
public int X { get; set; }
public int Y { get; set; }
} //the user customer data type.
Point p = new Point() { X = , Y = };
//use the predefine generic tuple type.
Tuple<int, int> p2 = new Tuple<int, int>(, ); //
Console.WriteLine(p.X + p.Y);
Console.WriteLine(p2.Item1 + p2.Item2);

一个简单的包含两个Int类型成员的类,传统的方法定义point需要写很多代码,但是使用tuple却只有一句,组元多用于方法的返回值。如果一个函数返回多个类型,这样就不在用out , ref等输出参数了,可以直接定义一个tuple类型就可以了。非常方便

//1 member
Tuple<int> test = new Tuple<int>();
//2 member ( 1< n <8 )
Tuple<int, int> test2 = Tuple.Create<int, int>(,);
//8 member , the last member must be tuple type.
Tuple<int, int, int, int, int, int, int, Tuple<int>> test3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(, , , , , , , new Tuple<int>()); //
Console.WriteLine(test.Item1);
Console.WriteLine(test2.Item1 + test2.Item2);
Console.WriteLine(test3.Item1 + test3.Item2 + test3.Item3 + test3.Item4 + test3.Item5 + test3.Item6 + test3.Item7 + test3.Rest.Item1);

第一个定义包含一个成员。

第二个定义包含两个成员,并且使用create方法初始化。

第三个定义展示了tuple最多支持8个成员,如果多于8个就需要进行嵌套。注意第8个成员很特殊,如果有8个成员,第8个必须嵌套定义tuple。如果上面所示。

下面又举了两个嵌套定义的例子,简单的要命:

//2 member ,the second type is the nest type tuple.
Tuple<int, Tuple<int>> test4 = new Tuple<int, Tuple<int>>(, new Tuple<int>());
//10 member datatype. nest the 8 parameter type.
Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>> test5 = new Tuple<int, int, int, int, int, int, int, Tuple<int, int, int>>(, , , , , , , new Tuple<int, int, int>(, , )); //
Console.WriteLine(test4.Item1 + test4.Item2.Item1);
Console.WriteLine(test5.Item1 + test5.Item2 + test5.Item3 + test5.Item4 + test5.Item5 + test5.Item6 + test5.Item7 + test5.Rest.Item1 + test5.Rest.Item2 + test5.Rest.Item3);

本文源自:http://www.cnblogs.com/namek/archive/2010/08/16/1800366.html(还是实例下载)

下面的列子稍微复杂一点:

c# 组元(Tuple)的更多相关文章

  1. c#组元(Tuple)的使用

    组元(Tuple)是C# 4.0引入的一个新特性,可以在.NET Framework 4.0或更高版本中使用.组元使用泛型来简化类的定义,多用于方法的返回值.在函数需要返回多个类型的时候,就不必使用o ...

  2. 【转】NumPy-快速处理数据

    2.0 简介 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针(为了保存各种类型的对象,只能牺牲空间).这样 ...

  3. 语音激活检测(VAD)--前向神经网络方法(Alex)

    这是学习时的笔记,包含相关资料链接,有的当时没有细看,记录下来在需要的时候回顾. 有些较混乱的部分,后续会再更新. 欢迎感兴趣的小伙伴一起讨论,跪求大神指点~ VAD(ffnn神经网络)-Alex t ...

  4. NumPy-高速处理数据

    Numpy简单介绍 标准安装的Python中用列表(list)保存一组值,能够用来当作数组使用,只是因为列表的元素能够是不论什么对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1,2,3 ...

  5. C#高级编程第10版 note

    泛型接口的抗变和协变 https://www.cnblogs.com/yanfang/p/6635302.html ①泛型接口,如果泛型类型前没有关键字out或者in来标注,则该泛型接口不支持抗变和协 ...

  6. NumPy-快速处理数据--ndarray对象--多维数组的存取、结构体数组存取、内存对齐、Numpy内存结构

    本文摘自<用Python做科学计算>,版权归原作者所有. 上一篇讲到:NumPy-快速处理数据--ndarray对象--数组的创建和存取 接下来接着介绍多维数组的存取.结构体数组存取.内存 ...

  7. 5、Numpy处理数据

    转载自:http://old.sebug.net/paper/books/scipydoc/numpy_intro.html#id9 2 NumPy-快速处理数据 标准安装的Python中用列表(li ...

  8. 建议10:numpy使用基础

    # -*- coding: utf-8 -*- import numpy as np #---------------------------------------- #-- 定义 ndarray ...

  9. Numpy的那些事儿

    2 NumPy-快速处理数据 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1, ...

  10. python 数组学习

    2 NumPy-快速处理数据 标准安装的Python中用列表(list)保存一组值,可以用来当作数组使用,不过由于列表的元素可以是任何对象,因此列表中所保存的是对象的指针.这样为了保存一个简单的[1, ...

随机推荐

  1. matlab之图像处理(2)

    diagram = imread('lena1.png') diagram = rgb2gray(diagram);%------------------------------将图片转换为灰度图 N ...

  2. ECMAScript整理笔记(持续更新....)

    参考文献: ECMAScript Array:http://www.jimmycuadra.com/posts/ecmascript-5-array-methods ECMAScript5兼容展示大全 ...

  3. VMware Workstation中linux(centos)与windows7共享文件夹

    引用网站有: http://www.jb51.net/LINUXjishu/161994.html http://www.cnblogs.com/xiehy/archive/2011/12/19/22 ...

  4. JAVA 编码机制

    先看例子: public class Test { public static void main(String[] args) { char han = '永'; System.out.printl ...

  5. openerp 经典收藏 通过view实现字段的只读、隐藏操作(转载)

    通过view实现字段的只读.隐藏操作 原文地址:http://cn.openerp.cn/view_groups/ 在OpenERP V7视图(ir.ui.view)多了一个非常有用的字段(group ...

  6. 从零开始学ios开发(十四):Navigation Controllers and Table Views(上)

    这一篇我们将学习一个新的控件Navigation Controller,很多时候Navigation Controller是和Table View紧密结合在一起的,因此在学习Navigation Co ...

  7. LintCode-Majority Number

    Given an array of integers, the majority number is the number that occurs more than half of the size ...

  8. Careercup - Facebook面试题 - 5733320654585856

    2014-05-02 09:59 题目链接 原题: Group Anagrams input = ["star, astr, car, rac, st"] output = [[& ...

  9. Jquery 固定悬浮层以及固定表头

    /* =========================================================== * jquery.autofix_anything.js v1 * === ...

  10. 数据库SQLiteDatabase

    package com.baclock.entity; import android.provider.BaseColumns; /** * Created by Jack on 5/4/2016. ...