class Program { static void Main() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.Add(5); // Remove all except first 3 foreach (int i in list) { Console.Write(i); } } }
var firstThree = list.Take(3);
List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); list.Add(4); list.Add(5); int remove = Math.Max(0, list.Count - 3); list.RemoveRange(3,remove); foreach (int i in list) { Console.Write(i); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)