Nullable type cho phép value type nhận giá trị null: int? age = null;.
- Bên dưới nó sử dụng
Nullable<T>để bọc value type lại. - Dùng nullable type khi một giá trị có thể hợp lệ là vắng mặt, chẳng hạn trường tùy chọn hay dữ liệu null từ database.
- Kiểm tra null bằng thuộc tính
HasValuehoặc toán tử??để cung cấp giá trị mặc định.
Nullable types allow value types to hold null values: int? age = null;.
- They wrap value types in a
Nullable<T>container. - Use them when a value may legitimately be absent — optional fields or database nulls.
- Check nullability with the
HasValueproperty or the null-coalescing operator??to provide defaults.