In the domain of system-modeling (e, systemVerilog, matlab, phyton), lists are obsoleting arrays, stacks and queues(*) altogether. Other domains that use python, perl and ruby have that same mindset, as well.
I am a system-modeler and a List aficionado that is new to c# and java (for hi-school teaching). c# seem so great ..but...
The Remove&Return approach is respected by :
C# Stack, C# Queue, Java ArrayList ,Python list, Perl, Ruby, e, systemverilog and others.
A notable exception is ... C# List.
c#
List<T>.RemoveAt(idx)
doesn't return a value.
Why is that important ? Say one wants to post this in a modeling web site (who aren't experts in c#):
int len=myCriticalProcessList.Length; sendMethod( myCriticalProcessList.RemoveAt(len - 1) );
That seem to work for any modern language, but to post it in c#, one has to also post several more lines, and invent new names. Also one doesn't need to know the type of the list! And when the type is changed, he does not need to change his code at all. Also naturally System modeling folks care about advanced c#, as much as c# folks care about advanced system-modeling (i.e none).
Can there be any damage if a value is returned in that case?
Is there a performance consideration here?
Does Java-value-return in arraylist.remove, make it slower, or less safe?
Is c#'s remove&return approach good for Stack
, and Queue
, yet not a good idea for List
?
Is there some difference in underline implementation, or philosophy ?
Side note:
C# Stack.Pop
andc# Queue.Dequeue
do return a value. (a remove&return approach, like Java remove, and unlike c# RemoveAt).Python's
list.pop([idx])
, removes-element-by-position, and returns a value. Similar toremove
injava
( yet with a default, remove&return the last element).C++ (stl) remove, is also void (thanks @Deduplicator). Yet, decisions relevant for languages without native-built-in-garbage-collection, are not always relevant for GC langs.
Seem @Deduplicator comment points us to the answer!!
C# was inspired by c++ not only in it's name, but also in many other things.Additionally, C# compiler was developed in C++ (in it's first ~15 years). Find me one human that is not influenced by the language he is using ... No one says it's better than Mads Torgersen, the program manager of c# : "Working in C# every day makes you think differently about C#: It’s the power of “dogfooding”." https://medium.com/microsoft-open-source-stories/how-microsoft-rewrote-its-c-compiler-in-c-and-made-it-open-source-4ebed5646f98
(*) minor: queues are not 100% obliterated by lists. There're very rare cases in system modeling were O(n) for dequeue is not tolerated.
remove
, it ispop
. In any case, Java's preference to return the removed item in an ArrayList is just that: a preference, easily remedied in C# by simply getting the item from the list before removing it.