C# 8中基本数据类型的可空值类型
来源:互联网 发布:北京联合大学知乎 编辑:程序博客网 时间:2024/05/14 08:46
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;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); } }}
运行后结果如下所示:- C# 8中基本数据类型的可空值类型
- C#中数据类型的基本了解
- c#的基本数据类型与.NET Framework类型
- c#的基本数据类型
- 02 C# 第二章 数据类型 - 基本类型
- Java中基本数据类型和包装器类型的关系
- C++中类类型和基本数据类型的转换
- Java中基本数据类型和包装器类型的关系
- C# 中常用数据类型与控件类型的命名规则
- C# 中常用数据类型与控件类型的命名规则
- 数据库中字段类型对应的C#中的数据类型
- 数据库中字段类型对应的C#中的数据类型
- C#的数据类型整数类型
- java中基本数据类型和包装类型
- JavaScript中基本数据类型和包装类型
- 8中基本数据类型
- C#基本语法 - 基本的数据类型
- java基本数据类型-基本类型的类型转换
- spring mvc中的页面跳转
- 关于connect: network is unreachable 问题的解决
- C#基础教程(简单易懂)-神马是委托?
- 特殊构造(非捕获总结)
- HDU 3657 Game
- C# 8中基本数据类型的可空值类型
- the IObjectWithSiteImpl
- @ww.select 标签 属性
- ibm开发者文档
- error LNK2019: 无法解析的外部符号 该符号在函数 中
- SQL中运用事务实现多表更新操作
- hibernate3.5和J2EE包冲突
- 登陆三次失败输入验证码
- ORA-12541:TNS:无监听程序”错误解决