C# Interview Question #77
What are nullable reference types in C#?
Memory Management & Modern C# Mid-Level Intermediate
Quick Interview Answer
Nullable reference types are a compile-time analysis feature that helps detect possible null-reference problems.
Detailed Explanation
Nullable reference types are a compile-time analysis feature that helps detect possible null-reference problems.
When nullable annotations are enabled, string means the developer intends the value to be non-null, while string? indicates that null is an expected value.
The compiler produces warnings when code may dereference null or fails to initialize non-nullable members. This feature does not create a new runtime reference type; it improves static null-safety analysis.
Code Example
public class UserDto
{
public string Name { get; set; } = string.Empty;
public string? MiddleName { get; set; }
}