Search code examples
c#compiler-constructionprogramming-languagesnull

How to differentiate Null or Empty ? on what criteria Null and Empty treated?


Hi all of SO users I'm having little confusion with understanding null and Empty. I know null is special case, which is not equal to itself. I want to know how to determine that it is null, on what basis you are telling it is null, and the same for empty also

  1. Is it based on memory allocation or what?
  2. If then how memory allocated for null.
  3. If its empty then what crieteria suits for it to be as "EMPTY".

Just I want know how the compiler come to know it is NULL or Empty.


Solution

  • null is a special reference that means 'not referencing anything'

    Empty only applies to collections (and a string is a collection of char). It means there is a valid collection but it contains no (0) elements.

    When returning a collection it is usually better to return an empty collection instead of null.

    When receiving a collection, decide whether null should be treated as an empty collection. This depends on the situation, agreements and promises made etc.

    i know null is special case, which is not equal to itself

    Not entirely true. null == null is always true.