当前位置:网站首页>Available types in C #_ Unavailable type_ C double question mark_ C question mark point_ C null is not equal to
Available types in C #_ Unavailable type_ C double question mark_ C question mark point_ C null is not equal to
2022-06-13 02:55:00 【Tianma 3798】
C# Available types in _ Type not available _C# ??_C#?._C# null!
One 、 Null type Nullable
When we talk about null When it's safe , A variable can have two states :
- Nullable : It can be for null.
- Non-Nullable : Not for null.
from C# 8.0 Start , By default, all reference types are Non-nullable.
“ Emptiness ” This can be modified by the following two new type operators :
- ! : from Nullable Change it to Non-Nullable
- ? : from Non-Nullable Change it to Nullable
These two operators correspond to each other . You use these two operators to qualify variables , The compiler then ensures that... According to your qualifications null Security .
1、? Usage of operators
It is mainly used for corresponding databases null type .
Nullable:int? x;
x It's the basic data type , Therefore, by default, it cannot be null Of .
We use ? Operator to change it to be null Of .
x = null; The assignment is normal , No warning .
2、?. NULL Conditional operator
Person per = null;
string name = per?.Name;No error will be reported here .
?[ NULL Operators can not only use ?. Access object properties and methods , You can also use ?[ Monitors whether the array or the object containing the indexer is empty
int[] are = new int[5] { 2, 3, 5, 7, 4 };
int? value = are?.Length;
int? result = are?[0];3、?? Air transport operator
int[] are = new int[5] { 2, 3, 5, 7, 4 };
int value = are?.Length??0;
int result = are?[0]??0;Example :
Student stu2 = new();
string name = stu2.Name??" Zhang San ";
Console.WriteLine(name);// Print " Zhang San ", similar js in || Operator
Two 、 An uncontrollable variable of nullable type Non-Nullable / null!
null! assignment
null! It's telling the compiler null No null value , This method only prompts in the coding process , Errors may not be reported during operation .

namespace ConsoleApp1
{
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
public class School
{
// The current field cannot be set to empty
public Student StuInfo { get; set; } = null!;
}
}
Student stu=new Student();
stu.ID = 10;
stu.Name = " Zhang San ";
Console.WriteLine(stu.ToString());
School schOne = new School();
schOne.StuInfo = null;
Console.WriteLine(" End of code execution ");Compiler here , Tips schOne.StuInfo = null; Cannot be assigned to null.
more :
C# data type 、 Variable 、 Scope
C# Generic explanation _ Generic basis _C# Generic
lambda expression /Lambda expression /lambda expression Use finishing
边栏推荐
- Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
- Why does it feel that most papers still use RESNET as the backbone network rather than densenet?
- Detailed installation tutorial of MATLAB r2019 B-mode ultrasound (complete installation files are attached)
- OneNote User Guide (1)
- For loop instead of while loop - for loop instead of while loop
- Perfect square
- [data analysis and visualization] key points of data drawing 9- color selection
- Rounding in JS
- Entity framework extends the actual combat, small project reconfiguration, no trouble
- [data analysis and visualization] key points of data drawing 11- precautions for radar chart
猜你喜欢

Why does it feel that most papers still use RESNET as the backbone network rather than densenet?

Detailed explanation of UCI datasets and their data processing (with 148 datasets and processing codes attached)

The extra money we made in those years

OneNote User Guide (1)

Prometheus install and register services
![[life science] DNA extraction of basic biological experiments](/img/84/c1968c2c08feab44b14a529420eea9.jpg)
[life science] DNA extraction of basic biological experiments

How did you spend your winter vacation perfectly?

Six special GPU products for domestic aircraft passed the appraisal and review
![[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3](/img/b8/06779a82e79e44534573fa60460b31.jpg)
[data and Analysis Visualization] D3 introductory tutorial 2- building shapes in D3

二叉树初始化代码
随机推荐
Binary tree initialization code
nn. Conv2d and nn Convtranspose2d differences
Control scanner in Delphi
Implementing fillet in custom dialog
redis. Conf general configuration details
Opencv 9 resize size change rotate rotate blur mean (blur)
Ijkplayer source code ---setdatasource
Simple use of leaflet - offline map scheme
Node uses post to request req Pit with empty body
[data analysis and visualization] key points of data drawing 8- use of circular bar chart
Linked list: palindrome linked list
Ffmpeg principle
Ijkplayer source code - setting option 2
Ijkplayer source code - setting options
Summary of the latest IOS interview questions in June 2020 (answers)
PK of dotnet architecture
Use of OpenCV 11 kmeans clustering
Prometheus install and register services
Collection of IOS development interview and underlying learning videos
Matlab: obtain the figure edge contour and divide the figure n equally