c# 中的可空类型与空接合运算

本文详细介绍了C#中可空类型的概念与使用方法,包括如何处理可空类型的比较及运算,展示了空接合运算符的用法,并通过实例解释了可空类型与引用类型的区别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace CSharp基础
  6. {
  7.     /// <summary>
  8.     /// 可空类型的展示
  9.     /// </summary>
  10.     class 可空类型
  11.     {
  12.         public static void Main()
  13.         {
  14.             int? a = null;
  15.             int? c = a * 5;
  16.             // c = a * 5中因为a的值是null,所以c的值一定会是null
  17.             if (c == null)
  18.             {
  19.                 Console.WriteLine( "null");
  20.             }
  21.             //值为25
  22.             a = 5;
  23.             c = a * 5;
  24.             Console.WriteLine(c);
  25.             a = null;
  26.             //在可空类型比较时,只要有一个操作数是null,则结果一定是false
  27.             if (a > c)
  28.             {
  29.                 Console.WriteLine("a>c");
  30.             }
  31.             else
  32.             {
  33.                 Console.WriteLine( "a==null");
  34.             }
  35.             //空接合运算
  36.             Console.WriteLine( "-----------------空接合运算--------------------------");
  37.             //空接合运算符作用是处理 可空类型 与 引用类型 
  38.             // ?? 符号左边的数必须是 可空类型或者是引用类型,也就是必须是有可能==null的,值类型当然是不可以了
  39.             // ?? 符号右连的数,必须是一个与左边数型相同的值,或者可以隐式转换的值.
  40.             // 如果左边的数值不为null,则返回值是左连数,如果为null,则返回第二个数
  41.             //b为可空类型
  42.             int? b = null;
  43.             //因为b==null则返回值是10
  44.             int reInt = b ?? 10;
  45.             //b为可空类型
  46.             int? d = 20 ;
  47.             //因为b<>null,则返回b的值,也就是20
  48.             int reInt2 = d ?? 10;
  49.             //其实以上??运算符类似与IsNull方法的执行
  50.             int reInt3 = IsNull(b);
  51.             int reInt4 = IsNull(d);
  52.             Console.WriteLine( reInt );
  53.             Console.WriteLine( reInt2);
  54.             Console.WriteLine(reInt3);
  55.             Console.WriteLine(reInt4);
  56.             Console.WriteLine( "----------------引用类型的空接合运算演示--------------------");
  57.             Person per = null;
  58.             Person per2 = new Person();
  59.             per2.name = "aladdin";
  60.             Person rePer = per ?? per2;
  61.             Console.WriteLine( rePer.name );
  62.             Console.ReadLine();
  63.         }
  64.         public static int IsNull(int? par)
  65.         {
  66.             if (par == null)
  67.             {
  68.                 return 10;
  69.             }
  70.             else
  71.             {
  72.                 return (int)par;
  73.             }
  74.         }
  75.         class Person
  76.         {
  77.             public string name;
  78.         }
  79.     }
  80. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值