Last post Nov 12, 2017 06:02 PM by pankaj@dotnetlooker
Nov 09, 2017 04:44 AM|merrittr|LINK
I have code like this
public List<int> train = new List<int>();
but the 2 lines where I am assignmg train to itself reversed I get
Compiler output: TrainComposition.cs(29,16): error CS0029: Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<int style="box-sizing:border-box;color:#525252;font-family:Menlo, Monaco, Consolas, 'Courier New', monospace;font-size:15px;">' TrainComposition.cs(31,15): error CS0029: Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<int style="box-sizing:border-box;">'</int></int>
any idea what is wrong ... I am trying to add a list item to the left side is the point of the reversing
Nov 09, 2017 05:26 PM|PatriceSc|LINK
Reverse doesn't produce a new reversed list. The current list is updated. So in short your code should just be :
As a side note is this is your full code ? You are just testing ? It seems you'll run into another error (trying to access a non existing element plus reversing the list seems to serve no purpose at all).
Edit: ah missed what you needed. So you want to use (from
https://msdn.microsoft.com/en-us/library/sey5k5z4(v=vs.110).aspx ) :
train.InsertAt(wagonId,0); // will always insert at the beginning of the list without having to reverse/unreverse.
IMO when you start to work with a class which is not familiar always have a look at the doc to get an idea about its capabilities.
Nov 12, 2017 06:02 PM|pankaj@dotnetlooker|LINK
As per your code :
train=train.Reverse(); // It is giving the compilation error.
Reverse() function has void as a return type & your are trying to assign void value to variable train which is accepting list of integers. See below its definition.
It reverses the order of the elements in the entire System.Collections.Generic.List<T> so no need to assign it back to list.
See the demo code:
List<int> train = new List<int>();
train.Insert(0, 3); // or train = 5; // You can add wagonId either by giving array index to list or by using List<T> Insert() function where the first argument is index // of list & second argument is the item needs to be insert. train.Reverse();
foreach (var item in train)
Follow me@DotNet Looker