1. Basic use
1. The ? operator follows the type, indicating that the current variable can be null.
int a = null; //这句代码在有空安全时,编译会提示错误
What if you want to assign null to a variable? Only need to type
Add operator after? That's it, eg:
int? a = null;
At the same time, when we use a variable or method of a nullable object, we need to use it ?.
, but not .
, eg:
2. The ! operator follows the type.
The operator !
requires the developer to ensure that the variable is not null
, otherwise an exception will be thrown. eg:
here _socket will not be empty, so you can use it with confidence!.