Null means "no value" or "no data". You use nullable types when "no value" is a valid value in the context of whatever system you are desining/using.
int? weightOfCargo = TryGetTruckCargoWeight()In this case, the nullable type indicates that TryGetTruckCargoWeight() may return null. The meaning of this would genereally be that the data is unavailable (maybe there's no truck, maybe it hasn't been weighed, etc).For an example of when you might use a nullable type, consider how an ordinary Boolean variable can have two values: true and false. There is no value that signifies "undefined". In many programming applications, most notably database interactions, variables can exist in an undefined state. For example, a field in a database may contain the values true or false, but it may also contain no value at all. Similarly, reference types can be set to null to indicate that they are not initialized.
0 comments:
Post a Comment