C# keyword 'empty' compiles to the static Empty property of List, Dictionary, etc. #66964
-
Typically, when a function generates a list, instead of returning a NULL, it has been proposed as a best-practice to return a static empty list, preventing memory use for the creation of a new empty list, and allowing the caller to interate the list without concern about NULL. Perhaps a new keyword 'empty' could return the static property of the class with the name 'Empty'. As a convention, this may make this process easier to read.
This could be changed to
Of course, this might only work with types that specifically implement a property for Empty, such as dictionary, list, etc. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
The collection-literals proposal (such were working on for the next version of c#) allows you to just use |
Beta Was this translation helpful? Give feedback.
-
Does the collection-literals proposal use a single static instance of the empty collection, to prevent allocation memory on the heap for a new collection each time an empty is returned? |
Beta Was this translation helpful? Give feedback.
The collection-literals proposal (such were working on for the next version of c#) allows you to just use
[]
to represent 'empty' in all these cases.