可空值类型

C#中的可空值类型

C#不允许把NULL值赋给一个值类型,以下语法是错误的

int i a =null;

但是,利用 C# 定义的一个修饰符,可将一个变量声明为一个可空(nullable)值类型。可空值类型在行为上与普通值类型相似,但可以将一个 null 值赋给它。如下所示:

int? a = null;      // 合法

当把一个变量定义为可空值类型时,该变量依然可以被赋值为 0,代码如下所示:

using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace 可空类型  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            int? a = null;  
  
            Console.WriteLine("a = {0}", a);  
            a = 0;  
            Console.WriteLine("a = {0}", a);  
        }  
    }  

C# 8中基本数据类型的可空值类型

 

C# 8中基本数据类型除了 string (string是引用类型)外,int、long、float、double、decimal、char、bool (这7中都是值类型)都可以声明为可空值类型。且在方法中使用时,参数位置也没有可空值类型必须在非可空值类型后面的限制,可空值类型可以定义在方法参数列表的前中后任何位置。

 

        struct(结构) 类型是值类型,也可以声明为可空值类型。

 

        对于 string 类型,可以用 string.Empty 输出空值。另外,除了 string.Empty 外,string 类型也可直接赋值为 null。如下:

string str = null;  // 合法  
string str = string.Empty;  // 合法  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
  
namespace 可空类型  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            Student student1 = new Student();  
  
            student1.StudentInformation(12, "boy", 18, 180, 86.0F,   
                90.0, 95.0M, 85.0F, "Steven", 'A', true, 168);  
  
            Console.WriteLine();  
  
            student1.StudentInformation(12, "boy", null, null, null,   
                null, null, 85.0F, "Steven", null, null, 168);  
  
            Console.WriteLine();  
  
            student1.StudentInformation(12, string.Empty, null, null, null,       
                null, null, 85.0F, string.Empty, null, null, 168);  
  
                // 对于 string 类型,可以用 string.Empty 输出空值  
  
            Console.WriteLine();  
  
            student1.StudentInformation(12, null, null, null, null,  
                null, null, 85.0F, null, null, null, 168);  
  
                // 把 赋值给 string 类型的 string.Empty   
                // 换成 null 后可得到同样的输出  
        }  
    }  
  
    class Student  
    {  
        //public Student()  
        //{  
        // 默认构造器注释掉,依然可以运行,实际上程序会自己建一个隐藏的默认构造器  
        //}         
        public void StudentInformation(  
            int schoolAge,  
            string sex,  
            int? age,  
            long? height,  
            float? mathScore,  
            double? biologyScore,  
            decimal? geographyScore,  
            float artScore,  
            string name,  
            char? scoreGrade,  
            bool? passed,   
            int ID)  
        {  
            Console.WriteLine("Name:            {0}", name);  
            Console.WriteLine("ID:              {0}", ID);  
            Console.WriteLine("Sex:             {0}", sex);  
            Console.WriteLine("Age:             {0}", age);  
            Console.WriteLine("SchoolAge:       {0}", schoolAge);  
            Console.WriteLine("Height:          {0}", height);  
            Console.WriteLine("MathScore:       {0}", mathScore);  
            Console.WriteLine("ArtScore:        {0}", artScore);  
            Console.WriteLine("BiologyScore:    {0}", biologyScore);  
            Console.WriteLine("GeographyScore:  {0}", geographyScore);  
            Console.WriteLine("ScoreGrade:      {0}", scoreGrade);  
            Console.WriteLine("Passed:          {0}", passed);  
        }  
    }  
}

 

转载于:https://www.cnblogs.com/MiyaQian/p/4159324.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值