You are looking for Enumerable.Empty<T>().
In other news the Java empty list sucks because the List interface exposes methods for adding elements to the list which throw exceptions.
Answer from Stilgar on Stack Overflow.net - How to empty a list in C#? - Stack Overflow
stack - How to empty a list - Stack Overflow
How to check if a linked list is empty in C - Stack Overflow
Check if list is empty in C# - Stack Overflow
It's really easy:
myList.Clear();
If by "list" you mean a List<T>, then the Clear method is what you want:
List<string> list = ...;
...
list.Clear();
You should get into the habit of searching the MSDN documentation on these things.
Here's how to quickly search for documentation on various bits of that type:
- List Class - provides the
List<T>class itself (this is where you should've started) - List.Clear Method - provides documentation on the method Clear
- List.Count Property - provides documentation on the property Count
All of these Google queries lists a bundle of links, but typically you want the first one that google gives you in each case.
One can declare to ignore the unused variable:
(defun emptystack ()
(dolist (var *stack*)
(declare (ignore var))
(pop *stack*)))
With the DO macro:
(defun emptystack ()
(do () ; no bindings
((null *stack*)) ; end test, no further results
(pop *stack*))) ; body
In LOOP one can also ignore the variable by naming it nil:
(defun emptystack ()
(loop for nil in *stack* do (pop *stack*)))
You could use
(defun emptystack ()
(loop while *stack* do (pop *stack*)))
which indeed does not use a variable under the hood:
? (macroexpand-1 '(loop while *stack* do (pop *stack*)))
(BLOCK NIL (ANSI-LOOP::LOOP-BODY NIL ((UNLESS *STACK* (GO ANSI-LOOP::END-LOOP))) ((POP *STACK*)) ((UNLESS *STACK* (GO ANSI-LOOP::END-LOOP))) NIL))
First of all you don't need double pointer. Initialization with NULL is important here. (Denotes the empty list).
SortedList *list = NULL;
This has the benefit that now if you want to make changes to it pass it's address.
func(&list);
And checking the empty list would be
void func(SortedList **list){
if( *list == NULL )
/* empty */
}
This is usually the way lists are implemented.
In your case you simply allocated to a local variable and instead of returning the memory address you have overwritten the pointer's value with NULL - there is memory leak in the code.
With your code it will be something like. Yes all that the initialization does is assigning it to NULL. This is one easy way out towards checking the NULL. At the very beginning making it NULL
basically denotes that list is empty.(which is the case generally).
list is a pointer to a pointer. The actual list is in the value it points to, so you need to dereference it.
An empty list is just a null pointer, so you don't need to allocate anything in init().
void init(SortedList** list)
{
*list=NULL;
printf("%p\n", *list);
}
void isEmpty(SortedList** list)
{
printf("%p\n", *list);
if(*list == NULL) printf("List is empty\n");]
}
And in the main function, you don't need a double pointer, you use the & (address-of) operator to get the address of a pointer variable.
int main()
{
SortedList *list;
init(&list);
printf("Initialization with succes\n");
isEmpty(&list);
return 0;
}
You only really need double pointers for functions that modify the list. So you could use an ordinary pointer for isEmpty:
void isEmpty(SortedList* list)
{
printf("%p\n", list);
if(list == NULL) printf("List is empty\n");]
}
and then call it as:
isEmpty(list);
But it's probably simplest to be consistent across all your functions.
You can use Enumerable.Any:
bool isEmpty = !list.Any();
if(isEmpty)
{
// ...
}
If the list could be null you could use:
bool isNullOrEmpty = list?.Any() != true;
If the list implementation you're using is IEnumerable<T> and Linq is an option, you can use Any:
if (!list.Any()) {
}
Otherwise you generally have a Length or Count property on arrays and collection types respectively.