Nullable integer in .NET.

what is an integer with a zero value and where can it be used?

+7
types nullable
source share
6 answers

An integer with a null int? value int? or Nullable<int> is a value type in C #, the value of which can be null or an integer value. It defaults to null instead of 0 and is useful for representing things like value not set (or what you want it to represent).

+15
source share

A single integer with a zero value can be used in various ways. It may be null or value. Like here:

 int? myInt = null; myInt = SomeFunctionThatReturnsANumberOrNull() if (myInt != null) { // Here we know that a value was returned from the function. } else { // Here we know that no value was returned from the function. } 

Say you want to know the age of a person. It is in the database if the person has submitted his age.

 int? age = GetPersonAge("Some person"); 

If, like most women, a man has not submitted his age, then the database will contain zero.

Then you check the age value:

 if (age == null) { // The person did not submit his/her age. } else { // This is probably a man... ;) } 
+8
source share

Im implying that you mean int? x; int? x; , which is an abbreviation for Nullable<int> x;

This is a way to add zeros to value types and is useful in scenarios such as database processing, where values ​​can also be set to null. By default, the nullable type contains null, and you use HasValue to check if it has been set to a value. You can assign values ​​and use them as a regular value type.

0
source share

Int? year;

you can put them in your actions with the controller, and if the URL does not contain the year of the year, it will be zero, and not the default - 0, which may be a valid value for your application.

0
source share

A type with a null value will take its own type of value and an additional value of null.Useful for databases and other data types that contain elements that cannot be assigned a value.

0
source share

Whenever you cannot predict the outcome or exit of a particular business logic, go with null types.

The default type (example. Int) cannot be null. It fixes an error when trying to assign a null value. But types with a null value will not do this.

Thanks.

0
source share

All Articles