What Is Null, Nullable Types In C#

In programming, avoiding unnecessary variables and object creation is considered to be a good practice. Whenever we create a variable or object, memory is allocated to it. In C#, if no memory is allocated to a variable then it has to be null.




What Is null In C#?


In layman's language null means nothing or something which doesn't have a value. Similarly, in C# null keyword specifies that it doesn't have any value and doesn't point to anything in memory. By default, all the reference type variables are of null type and if we don't assign value to them then null value will be assigned to them automatically.

null exactly isn't a value, its just a notation to shown that the variable doesn't contain or point to anything.

Shown below is an example of a string array which has been given null value

string[] arrNames = null;

//Also even if you don't assign null to it,
//then also null will be automatically assigned to it as it is a reference type variable. 
//Below line is exactly the same as above for .Net framework.

string[] arrNames;

What Are Nullable Types?


Nullable types are those type of variables which can be assigned null value. In C#, if we want to assign null to any value type variable (of type int, long etc) then the compiler will show error as value type has to contain some value as their memory is pre-allocated when the application starts. So consider below example of an integer being assigned null.

int i = null;
//Compiler shows an error saying
//"Cannot convert null to 'int' because it is a non-nullable value type.

What if you want to assign null to int variable in some specific scenario?


Well! we have a scenario for you. Suppose you have DB wherein an integer valued column has no value in it. So while fetching the record you need to identify whether the column has value or not. So C# provides us a way to make that int variable of nullable type. Any value type can be converted to nullable type by appending '?' to the variable type keyword. Like int? or long?. Consider below example

int i = null; //Compiler error
int? i = null; //No error

Whenever '?' is used in such fashion, compiler understands it automatically that this value type is now inherited from Nullable<T> type of .Net framework. However, these nullable types are not similar to other variables in terms of performance because it occupies the space for 2 values - i.e. the value itself and a boolean value to indicate whether the value is null or not. So these should be used only when required.

Nullable types can contain the default value range of various data types as well as null. To check whether a int? or long? contain some value we use .HasValue property of the variable.

int? i =null;
bool isValuePresent =  i.HasValue; //returns False

Null Coalescing Operator


Have you came across some operator like double question mark '??' in code somewhere? Maybe you noticed it in our Operators Post. This odd looking operator is null coalescing operator. This operator is used in between two operands. It returns value of left operand if it's not null, if left operand is null then value of right operand is returned. Consider below example wherein we are assigning a value to string variables

string nullString;
string firstString = nullString ?? "CheezyCode";
Console.WriteLine(firstString); //CheezyCode

string secondString = firstString ?? "Test String";
Console.WriteLine(secondString); //CheezyCode

Here while assigning value to firstString, Null Coalescing Operator returned "CheezyCode" as value because nullString is null. Now while assigning value to secondString, firstString is not null so the '??' operator returned value from firstString i.e." CheezyCode" instead of "Test String"

So that's it about Null and its things. Check out previous and next topics of this series. Let us know if you have any query or concerns. Happy Reading (refer full series here - C# Tutorial Index )


Comments

Popular posts from this blog

Create Android Apps - Getting Started

Polymorphism in Kotlin With Example