Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't C# LinkedList.RemoveFirst() return the removed value?

Is there some idiomatic, performance or design philosophy reason why C#'s LinkedList's RemoveFirst() and RemoveLast() operations don't return the value removed?

Right now, if I want to read and remove the first value, I believe the incantation is:

LinkedList<string> list = ...;
...
string removed = list.First.Value;
list.RemoveFirst();

In Java, it would be:

LinkedList<String> list = ...;
...
String removed = list.removeFirst();

Don't get me wrong; I am not trying to say Java is better. C#'s LinkedList has many more affordances, simply by exposing the Node as a public construct. I am trying to understand the design choices.

like image 625
Dilum Ranatunga Avatar asked May 12 '11 18:05

Dilum Ranatunga


People also ask

Why there is no string in C?

Both Java and Python have the concept of a "string", C does not have the concept of a "string". C has character arrays which can come in "read only" or manipulatable. A character array is a sequence of contiguous characters with a unique sentinel character at the end (normally a NULL terminator '\0' ).

What does C++ have that C doesnt?

C++ was developed by Bjarne Stroustrup in 1979. C does no support polymorphism, encapsulation, and inheritance which means that C does not support object oriented programming. C++ supports polymorphism, encapsulation, and inheritance because it is an object oriented programming language.

Why C has no exception handling?

As such, C programming does not provide direct support for error handling but being a system programming language, it provides you access at lower level in the form of return values. Most of the C or even Unix function calls return -1 or NULL in case of any error and set an error code errno.

Why is C not outdated?

The C programming language doesn't seem to have an expiration date. It's closeness to the hardware, great portability and deterministic usage of resources makes it ideal for low level development for such things as operating system kernels and embedded software.


2 Answers

I can't really give a definitive answer, as I can't read the minds of the designers of LinkedList<T>. What I can say is this.

In Java, the LinkedList<E> class implements the Queue<E> interface, which reflects a decision on the designers' part: "You know what? A linked list can easily be used as a queue, so we might as well have it implement that interface." And the way you interact with a queue is by popping items off the end, and then, you know, using them for something (which means it's natural for a Pop-like operation to return the element popped).

In .NET, there is no IQueue<T> interface. Basically, the designers made a different decision: "The most efficient implementation of queue-like behavior we know of is a simple array-based circular queue. So if developers want a queue, they should use the Queue<T> class, which is exactly that."

If a developer wants to use a LinkedList<T> as a queue (or a deque for that matter), chances are he/she is picking the wrong implementation for the data structure he/she actually needs (from the .NET point of view).

Thus, in the spirit of "a proper function should do exactly one thing," the BCL folks opted to make LinkedList<T>.RemoveFirst do just that: remove the first element (similar to how List<T>.RemoveAt just removes the element at the specified index and returns nothing).

I'm not saying either decision is right or wrong. I think the different interfaces of the standard linked list class in Java and .NET simply reflect different views of what a linked list is and how it should be used within the two frameworks.

like image 129
Dan Tao Avatar answered Sep 25 '22 12:09

Dan Tao


Have you considered using a Queue or a Stack collection instead of a LinkedList? you can then push and pop and get the behavior you desire.

like image 20
Bueller Avatar answered Sep 23 '22 12:09

Bueller