0
what is the different between Null & Empty?!
for example in C# we have string.IsNullOrEmpty()!! i wonder if both Null & Empty dont have value, so why they used like that?! Please explain this for me💙
3 Réponses
+ 3
Mr.Kaveh
It is used like that because we want to check if the variable is null or empty. The variable will have garbage value if we don't assign it with null
For example
Input : str = null
// initialize by null value
String.IsNullOrEmpty(str)
Output: True
/*****************************/
Input : str = String.Empty
// initialize by empty value
String.IsNullOrEmpty(str)
Output: True
/**==========*************/
// We need to assign a variable either some value or null value
// if we do not initialize a variable it will take some garbage value. Garbage value is the value which is already present in a memory location.
As we know that there are many programs run on a computer. So who knows what value that particular memory location is holding.
// So when we assign a variable to null then it simply means that the particular memory location has nothing in it ( no garbage )
// Empty means 0
// Null means no existence of anything not even 0
+ 1
Null refers to something that does not exist. But empty refers to something that has 0 length.
+ 1
Please tag relevant languages only, the method you mentioned (string.IsNullOrEmpty) is C# specific. In other languages it may not be available, or may require some extra steps to confirm the object (being null or empty).
https://code.sololearn.com/W3uiji9X28C1/?ref=app