zoukankan      html  css  js  c++  java
  • C# 运算法 “??” “:: ” “int?”

    ?? 运算符(C# 参考)

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

    View Code
     1 class NullCoalesce
     2 {
     3     static int? GetNullableInt()
     4     {
     5         return null;
     6     }
     7 
     8     static string GetStringValue()
     9     {
    10         return null;
    11     }
    12 
    13     static void Main()
    14     {
    15         // ?? operator example.
    16         int? x = null;
    17 
    18         // y = x, unless x is null, in which case y = -1.
    19         int y = x ?? -1;
    20 
    21         // Assign i to return value of method, unless
    22         // return value is null, in which case assign
    23         // default value of int to i.
    24         int i = GetNullableInt() ?? default(int);
    25 
    26         string s = GetStringValue();
    27         // ?? also works with reference types. 
    28         // Display contents of s, unless s is null, 
    29         // in which case display "Unspecified".
    30         Console.WriteLine(s ?? "Unspecified");
    31     }
    32 }

    :: 运算符(C# 参考)

    命名空间别名限定符 (::) 用于查找标识符。 它通常放置在两个标识符之间,例如:

    global::System.Console.WriteLine("Hello World");

    ===============================================================================

    (下面内容转至http://www.cnblogs.com/firstcsharp/archive/2011/12/11/2283797.html

    int?:表示可空类型,就是一种特殊的值类型,它的值可以为null
    用于给变量设初值得时候,给变量(int类型)赋值为null,而不是0
    int??:用于判断并赋值,先判断当前变量是否为null,如果是就可以赋役个新值,否则跳过

    1 public int? a=null2 public int b()
    3 {
    4 return this.a ?? 0;
    5 }

    值类型后面加问号表示可为空null(Nullable 结构)

    Nullable是.NET 2.0中新提供的一种用于标明一个值类型是否可以为空的技术。

    对于一个类型,如果既可以给它分配一个值,也可以给它分配空引用null(表示没有任何值),我们就说这个类型是可空的。

    因此,可空类型可表示一个值,或表示不存在任何值。例如,类似 String 的引用类型就是可空类型,而类似 Int32 的值类型不是可空类型。Nullable 结构支持将值类型扩展为可以为null,但不支持在引用类型上使用,因为引用类型本身就是可空的。

    因为值类型的容量只够表示适合于该类型的值,因此它不可为空;值类型没有表示空值所需的额外容量。

    例:public int? age;

    补充:其它的类型后加问题也是同样的道理。
    int? num = null;   正确
    int num=null;     错误
    ---------------------------------------------------------------------------------------------------------------------------
    微软MSDN是提示如下例子:
    http://msdn.microsoft.com/zh-cn/support/1t3y8s4s(VS.80).aspx

     1  class NullableExample
     2     {
     3         static void Main()
     4         {
     5             int? num = null;
     6             if(num.HasValue == true)
     7             {
     8                 System.Console.WriteLine("num = " + num.Value);
     9             }
    10             else
    11             {
    12                 System.Console.WriteLine("num = Null");
    13             }         //y is set to zero   
    14             int y = num.GetValueOrDefault();
    15             // num.Value throws an  InvalidOperationException if num.HasValue is false       
    16             try
    17             {
    18                 y = num.Value;
    19             }
    20             catch(System.InvalidOperationException e)
    21             {
    22                 System.Console.WriteLine(e.Message);
    23             }
    24         }
    25     }

    以上将显示输出:

    num = Null

    Nullable object must have a value.

    可空类型概述
    可空类型具有以下特性:

    可空类型表示可被赋值为 null 值的值类型变量。无法创建基于引用类型的可空类型。(引用类型已支持 null 值。)。

    语法 T? 是 System.Nullable<T> 的简写,此处的 T 为值类型。这两种形式可以互换。

    为可空类型赋值与为一般值类型赋值的方法相同,如 int? x = 10; 或 double? d = 4.108;。

    如果基础类型的值为 null,请使用 System.Nullable.GetValueOrDefault 属性返回该基础类型所赋的值或默认值,例如 int j = x.GetValueOrDefault();

    请使用 HasValue 和 Value 只读属性测试是否为空和检索值,例如 if(x.HasValue) j = x.Value;

    如果此变量包含值,则 HasValue 属性返回 True;或者,如果此变量的值为空,则返回 False。

    如果已赋值,则 Value 属性返回该值,否则将引发 System.InvalidOperationException。

    可空类型变量的默认值将 HasValue 设置为 false。未定义 Value。

    使用 ?? 运算符分配默认值,当前值为空的可空类型被赋值给非空类型时将应用该默认值,如 int? x = null; int y = x ?? -1;。

    不允许使用嵌套的可空类型。将不编译下面一行:Nullable<Nullable<int>> n;

  • 相关阅读:
    JUC-狂神笔记整理学习
    多线程-学习笔记
    Redis分布锁
    Redis
    springcloud一个简单的基本流程
    Nacos
    mysql单表查询
    mysql多表查询
    mysql数据库
    mysql详细安装教程以及1067错误代码解决方案
  • 原文地址:https://www.cnblogs.com/cpcpc/p/2457107.html
Copyright © 2011-2022 走看看