(1)、C#语法中一个个问号(?)的运算符是指:可以为 null 的类型。

MSDN上面的解释:

在处理数据库和其他包含不可赋值的元素的数据类型时,将 null 赋值给数值类型布尔型以及日期类型的功能特别有用。例如,数据库中的布尔型字段可以存储值 true 或 false,或者,该字段也可以未定义。

(2)、C#语法中两个问号(??)的运算符是指null 合并运算符,合并运算符为类型转换定义了一个预设值,以防可空类型的值为Null。

MSDN上面的解释:

?? 运算符称为 null 合并运算符,用于定义可以为 null 值的类型和引用类型的默认值。如果此运算符的左操作数不为 null,则此运算符将返回左操作数(左边表达式);否则当左操作数为 null,返回右操作数(右边表达式)。

C# Code:

int? x = null;//定义可空类型变量
int? y = x ?? 1000;//使用合并运算符,当变量x为null时,预设赋值1000

Console.WriteLine(y.ToString()); //1000

/// <summary>
        /// Gets a single instance
        /// </summary>
        public static Log LogInstance
        {
              get

{

return _log ?? (_log = new Log()); //如果此运算符的左操作数不为 null,则此运算符将返回左操作数;否则返回右操作数。
               }
        }

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

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


 1 public class Point
2 {
3 public int X { get; set; }
4 public int Y { get; set; }
5 }
6
7
8 //the user customer data type.
9   Point p = new Point() { X = 10, Y = 20 };
10 //use the predefine generic tuple type.
11   Tuple<int, int> p2 = new Tuple<int, int>(10, 20);
12
13 //
14   Console.WriteLine(p.X + p.Y);
15 Console.WriteLine(p2.Item1 + p2.Item2);

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

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


 1             //1 member
2   Tuple<int> test = new Tuple<int>(1);
3 //2 member ( 1< n <8 )
4   Tuple<int, int> test2 = Tuple.Create<int, int>(1,2);
5 //8 member , the last member must be tuple type.
6   Tuple<int, int, int, int, int, int, int, Tuple<int>> test3 = new Tuple<int, int, int, int, int, int, int, Tuple<int>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int>(8));
7
8 //
9   Console.WriteLine(test.Item1);
10 Console.WriteLine(test2.Item1 + test2.Item2);
11 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。如果上面所示。

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


1             //2 member ,the second type is the nest type tuple.
2   Tuple<int, Tuple<int>> test4 = new Tuple<int, Tuple<int>>(1, new Tuple<int>(2));
3 //10 member datatype. nest the 8 parameter type.
4   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>>(1, 2, 3, 4, 5, 6, 7, new Tuple<int, int, int>(8, 9, 10));
5
6
7 //
8   Console.WriteLine(test4.Item1 + test4.Item2.Item1);
9 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);

c#编程指南(四) 组元(Tuple)的更多相关文章

  1. View Programming Guide for iOS ---- iOS 视图编程指南(四)---Views

    Views Because view objects are the main way your application interacts with the user, they have many ...

  2. JavaScript面向对象编程指南(四) 对象

    第4章 对象 4.1 从数组到对象 对象的组成:变量名.{}.用逗号分割的属性.用冒号分割的键/值对. var f={ name:'alen', // 可以在属性名上加引号 age:12 }; 对象文 ...

  3. libuv 中文编程指南

    最近看了一些有关 libuv 的东西,另外复习了一些与同步.异步.阻塞.非阻塞,异步IO(aio)的东西, 算是技术积累吧,等有时间了整理出一个完整的文档出来,希望在今后的编程中用到. 不多说了,本文 ...

  4. Apache Spark 2.2.0 中文文档 - Spark 编程指南 | ApacheCN

    Spark 编程指南 概述 Spark 依赖 初始化 Spark 使用 Shell 弹性分布式数据集 (RDDs) 并行集合 外部 Datasets(数据集) RDD 操作 基础 传递 Functio ...

  5. Spark编程指南V1.4.0(翻译)

    Spark编程指南V1.4.0 ·        简单介绍 ·        接入Spark ·        Spark初始化 ·        使用Shell ·        在集群上部署代码 ...

  6. 转-Spark编程指南

    Spark 编程指南 概述 Spark 依赖 初始化 Spark 使用 Shell 弹性分布式数据集 (RDDs) 并行集合 外部 Datasets(数据集) RDD 操作 基础 传递 Functio ...

  7. storm-kafka编程指南

    目录 storm-kafka编程指南 一.原理及关键步骤介绍 (一)使用storm-kafka的关键步骤 1.创建ZkHosts 2.创建KafkaConfig 3.设置MultiScheme 4.创 ...

  8. storm编程指南

    目录 storm编程指南 (一)创建spout (二)创建split-bolt (三)创建wordcount-bolt (四)创建report-bolt (五)创建topo storm编程指南 @(博 ...

  9. C++ 并发编程指南(收藏笔记)

    git地址: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice https://github.com/forhappy/Cpl ...

随机推荐

  1. Keil V4.72升级到V5.1X之后

    问题描述 Keil V4.72升级到V5.1x之后,原来编译通过的工程,出现了如下错误: .\Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\STM32f ...

  2. Asp.Net MVC过滤器小试牛刀

    在上学期间学习的Asp.Net MVC,基本只是大概马马虎虎的了解,基本处于知其然而不知其所以然.现在到上班,接触到真实的项目,才发现还不够用,于是从最简单的过滤器开始学习.不得不说MVC的过滤器真是 ...

  3. SSL与TLS的区别以及介绍

    转载 :http://kb.cnblogs.com/page/197396/ SSL:(Secure Socket Layer,安全套接字层),位于可靠的面向连接的网络层协议和应用层协议之间的一种协议 ...

  4. IntelliJ IDEA 部署Tomcat及创建一个web工程

    一.部署Tomcat 二.新建一个web工程 1.新建一个Project 2.现在建立一个简单的web工程,所以只勾选下面选中的,此外,本版本(IntelliJ IDEA 14.1.5只支持3.1版本 ...

  5. Android开发之onClick事件的三种写法(转)

    package a.a; import android.app.Activity; import android.os.Bundle; import android.view.View; import ...

  6. Spark的TorrentBroadcast:实现

    依据Spark 1.4版 序列化和反序列化 前边提到,TorrentBroadcast的关键就在于特殊的序列化和反序列化设置.1.1版的TorrentBroadcast实现了自己的readObject ...

  7. 网络爬虫-url索引

    网络爬虫-url索引 http://www.cnblogs.com/yuandong/archive/2008/08/28/Web_Spider_Url_Index.html url索引的作用是判断一 ...

  8. Java中List的排序

    第一种方法,就是list中对象实现Comparable接口,代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 2 ...

  9. laravel5的坑

    以此记录学习laravel的一些问题 问题:laravel转移文件夹到另外一pc或者环境后访问出现500 设置权限为777 问题: 设置路由后页面总是404 not found 解决:需要在apach ...

  10. javascript高级程序设计读书笔记

    第2章  在html中使用javascript 一般都会把js引用文件放在</body>前面,而不是放在<head>里, 目的是最后读取js文件以提高网页载入速度. 引用js文 ...