当前位置:网站首页>Deep understanding of c # nullable types
Deep understanding of c # nullable types
2022-07-29 11:07:00 【weixin_44326283】
Why can't value type variable is empty
nullIn the memory is full0存在的.Value type of the variable values stored in memory will automatically add a fully behind the value0The value of is used to limit the value range of value type variables.Reference types do not have this mechanism,So reference types can be null,while the value type cannot be null.But because the database of all types can be empty,So hopefully the value type can be null.
C#1The way to give the value type is empty
| 名称 | 具体操作 |
| 魔值 | Specific a number as a null value,例如DateTimeA null value in the database is passed to the database as0001-01-01 00:00:00 |
| reference type wrapper | boxing and unboxing |
| Additional non-duo logo | Use another value type for null value judgment |
Nullable<T>
属性1:HasValue,判断是否有值
属性2:Value,真实的值
方法GetValueOrDefault,If there is a real value, return that value,如果没有就返回默认值,You can add a value type parameter as a default value to the formal parameter of the method.
C#A hollow value is equal to a null value or no value,A null value is less than any other value.
C#2Syntactic sugar for nullable types
When the declaration value types,使用?修饰符,例如:int? i; This method is actually a simplified operation to be boxed value type.
Nullable types accept implicit conversions of any type,But nullable types must be explicitly converted to other types,and cannot be null when converted to pinch type.
C#中的??The operator can be seen as a simplified version of the ternary operation that evaluates to null.例如:
int? a = 4;
int b = 3;
int c = a ?? b;a为空时,c等于b;a不为空时,c等于a.其中amust be a nullable type otherwise it is meaningless,如果c为值类型,那么amust have a value orb必须为值类型.
??operators can also be used together,例如:
int? a = 3;
int? b = 2;
int c = 3;
int d = a ?? b ?? c;The execution mechanism of the entire statement is the same as above,But is the start of the first from the right,start judgingb和c(ps:I felt very anti-human at first.,Because other operators start from the left first).
总结:To assign a null value type variables,add just after the modifier when declaring?,使用??can have a sorting effect.
边栏推荐
- 就这?TypeScript其实并不难!(建议收藏)
- The heavyweight foundation awarded platinum, gold and silver donors
- INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
- 深入理解C# 可空类型
- 「PHP基础知识」使用数组保存数据
- 幸运抽奖系统带后台源码
- PyQt5快速开发与实战 6.6 QFormLayout(表单布局) && 6.7 嵌套布局 && 6.8 QSplitter
- Similarities and differences of QWidget, qdialog and qmainwindow
- 【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码
- [image detection] Research on cumulative weighted edge detection method based on gray image, with matlab code
猜你喜欢

Lucky draw system with background source code

How to start writing helm charts for your kubernetes application

Understand what a binary tree is (types, traversal methods, definitions of binary trees)

牛客网刷题

【图像检测】基于灰度图像的积累加权边缘检测方法研究附matlab代码

JVM知识点详细整理(长文警告)

Watch the open source summit first | quick view of the sub Forum & Activity agenda on July 29

重磅 | 2022 开放原子全球开源峰会在北京开幕

自采集在线电脑壁纸php源码v2.0自适应端

Getting started with pytoch
随机推荐
Understand what a binary tree is (types, traversal methods, definitions of binary trees)
【图像处理】基于中轴变换实现图像骨架提取附matlab代码
Conference OA project - my approval
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
深入理解C# 进入快速通道的委托
Spark高效数据分析01、idea开发环境搭建
Self collection online computer wallpaper PHP source code v2.0 adaptive end
Niuke net brush questions
golang 实现文件上传下载
ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching
How to use grep to display file names and line numbers before matching lines
幸运抽奖系统带后台源码
Luogu p4185 [usaco18jan]mootube g problem solution
Leetcode bit operation
专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
Pyqt5 rapid development and practice 6.6 qformlayout & 6.7 nested layout & 6.8 qsplitter
JVM知识点详细整理(长文警告)
就这?TypeScript其实并不难!(建议收藏)
WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
ES6 arrow function this points to