Click here to Skip to main content
15,890,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Experts
I have a question about LinkedList.
To concatenate two LinkedList, There is method called concat but it returns new Linkedlist.

Why so?

As I know it is very simple task, just need to assign first linkedlist last node with another linkedlist first node.

My question is why .Net Framework not following this way?


Thanks in advance
Posted

This way you avoid losing any of the combined lists. If you for example have a list of passed and failed you can easily create a complete list without losing the initial two linked lists. This is the same as if you would do:
String s = StringA + StringB; 

In this case the StringA and/or StringB isn't lost because s is used to keep the result. This is way faster and shorter than:
String s = new String(StringA);
s.Concat(StringB);


Because of the use of the '=' sign it is very clear that there is data assigned to it. It is easier to spot than calling a method that assigns all the items to the object.

Good luck!

Ps. Maybe an extra consideration (cannot see into the actual .net code but I would optimize this way) could be that creating a new linked list with the results of both gives the opportunity to get all the linked list references scattered through memory and put them into sequential order. A linked list is a kind of list that is crafted to work without the need for that but of course would it help improve memory caching enormously. So this might then look like a waste of time but this simple copying once would speed up the look up actions and therefore , on average, worth doing.
 
Share this answer
 
v2
I'm not very sure... but I think it has something to with genericity. I mean concatenating two linked lists might seen very logical but concatenating two Strings or Arrays might not be logical for practical (arrays) and security (strings) reasons. It think that the .Net Framework is simply trying to make things uniform especially because all lists have the same interface. In other words, you want all lists to behave the same way so that you can change the implementation without changing the interface.

I hope this helps
 
Share this answer
 
Comments
Khaniya 14-Sep-10 9:20am    
but I think it has something to with generosity


But They can check for type at compile time. So may be there should be another reason
Thanks for your response
I believe the Concat() method is an extension method in the System.Linq namespace. It works with any IEnumerable(T) collection and it returns an IEnumerable(T).

Don't ask me why there is no AddRange() or Concat() method of the LinkedList class...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900