Null and nullable types
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
null means "no object". Follow a seven-line program that keeps three variables honest: string? marks a reference that may be null, int? (Nullable<int>) gives a value type a HasValue flag, ?. and ?? turn a missing value into a safe default, ??= fills in a null, and the compiler, Roslyn, tracks which variables may be null so you fix a NullReferenceException before it ever happens. The program prints 33.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01string? name = null; What is n after var n = name?.Length ?? 0; ?
name?.Length checks name first. name is null, so the whole expression is null instead of throwing. Then ?? 0 replaces that null with 0, so n is the int 0.
02What is int? in C#?
A plain int is a value type and always holds a number. int? is the struct Nullable<int>, which adds HasValue; when HasValue is false the value reads as null.
03int? age = 5; age ??= 30; What is age now?
??= assigns only when the left side is null. age already has the value 5, so it is left alone.
04With nullable reference types on, what does Roslyn do with name.Length when name is a string? that may still be null?
Nullable warnings are warnings, not errors. If name really is null when the line runs, name.Length throws a NullReferenceException. Fix the warning with a check, a default, or by giving name a value first.
05int n = 3; int? age = null; What does n + age give?
C# lifts + to nullable types: if either operand is null, the result is null. Only when both have values does it add them.
المسرد
- null
- The reference to no object at all. It is the default value of every reference type, such as string.
- Reference type
- A type, such as string or a class, whose variables hold a reference (an address) to an object on the heap.
- Value type
- A type, such as int or bool, whose variables hold the value itself. A plain value type can never be null.
- Nullable value type (int?)
- Nullable<T>: a value type plus a HasValue flag. When HasValue is false, the value is null; read it with .Value or a default.
- Nullable reference type (string?)
- With <Nullable>enable</Nullable> (the default in new projects), string? says the variable may be null and string says it never should. Only the compiler's warnings change.
- ?. ?? ??=
- Null-conditional access (stops and gives null if the left side is null), null-coalescing (use the right side if the left is null), and null-coalescing assignment (assign only if null).
- Null-state analysis
- The compiler, Roslyn, follows each variable through the code to know where it may be null, and warns (such as CS8602) when you use it unchecked.
- NullReferenceException
- The exception thrown at run time when code uses a member, such as .Length, on a null reference.
الملاحظات والمصادر
- The program is real C#. With nullable reference types on (the default for new .NET 6+ projects), it builds with no warnings and prints 33.
- The variables row shows what the three local variables hold. Where the runtime keeps locals (stack slots or CPU registers) is its own choice.
- The arrow → "Ada" stands for the reference: the object's address. Safe C# code never sees the address itself.
- Nullable reference types are only annotations: they change the warnings, not the program. Nullable<int> is a real struct, so int? and int are different types at run time.
- Nullable warnings do not stop the build. To make them errors, add <WarningsAsErrors>nullable</WarningsAsErrors> to the project file.
- String literals such as "Ada" are interned: the runtime keeps one shared string object per literal, so line 5 loads a reference to that object instead of building a new one each time it runs (see String.Intern in the .NET docs).
- The null-forgiving operator ! (as in name!.Length) silences the warning without checking anything; if name is null it still throws.
- Microsoft Learn: Nullable reference types
- Microsoft Learn: Resolve nullable warnings (CS8602 and others)
- Microsoft Learn: Nullable value types
- Microsoft Learn: Member access operators, including ?. and ?[]
- Microsoft Learn: ?? and ??= operators
- Microsoft Learn: null keyword
- Microsoft Learn: Null-forgiving operator (!)
- Microsoft Learn: String.Intern (string literals are interned)
- Microsoft Learn: NullReferenceException class
- Microsoft Learn: Compiler options for errors and warnings (WarningsAsErrors)
اختبار يتاح بعد الخطوة الأخيرة.