C# : List

[".0s4*0|collections;datetime-format;list",["F@eCDNO^c","MAJAECBELABEMA","OOBCCSTUUUUTTUUUUYGDGDGDGDGDGDGDHDGDGDFOCOBOCBPGDHDFOBPGDHDFWSTTUUUUTTUUUU","rkwsr.",".NET","Array","Dictionary","List","String","2D","Async","Console","DataTable","Dates","DateTime","Enum","File","For","Foreach","Format","IEnumerable","If","IndexOf","Lambda","LINQ","Optimization","Parse","Path","Process","Property","Random","Regex","Replace","Sort","Split","Static","Substring","Switch","Tuple","While","Remove"," eliminates elements from List. The target elements may be found at certain indexes\u2014they may have certain values. They may instead match a condition we specify. The Remove method is effective for this.","Example."," To start, we show how to remove (erase) elements from your List. You can do this based on the element value you want to remove (with Remove), or based on the index (with RemoveAt). This is not equivalent to assigning an element to null. ","RemoveAt ","removeat","Next: ","This example shows Remove and RemoveAt. It removes the element with the value \"bulldog\", which erases the fourth element.","Then: ","It removes the element with index 1, which is the second dog, \"otterhound\".","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","C# program that uses Remove method","\n\nusing System;\nusing System.Collections.Generic;\n\nclass Program\n{\n static void Main()\n {\n List<string> dogs = new List<string>();\n dogs.Add(\"maltese\");"," // Contains maltese\n ","dogs.Add(\"otterhound\");"," // maltese, otterhound\n ","dogs.Add(\"rottweiler\");"," // maltese, otterhound, rottweiler\n ","dogs.Add(\"bulldog\");"," // ... rottweiler, bulldog\n ","dogs.Add(\"whippet\");"," // .... rottweiler, bulldog, whippet\n\n ","dogs.Remove(\"bulldog\");"," // Remove bulldog\n\n ","foreach (string dog in dogs)\n {\n Console.WriteLine(dog);\n }","\n // Contains: maltese, otterhound, rottweiler, whippet\n\n ","dogs.","RemoveAt","(1);"," // Remove second dog\n\n ","foreach (string dog in dogs)\n {\n Console.WriteLine(dog);\n }","\n // Contains: maltese, rottweiler, whippet\n ","}\n}\n\n","Output","\n\nmaltese\notterhound\nrottweiler\nwhippet\n\nmaltese\nrottweiler\nwhippet","Discussion."," Methods on List that remove certain elements require linear time. Therefore, it could be faster to assign null to elements you want to erase them, rather than removing them. But this could complicate your code. ","Note: ","RemoveAt is faster than Remove. It doesn't need to iterate through the number of elements equal to index.","Also,"," you can use RemoveAll to remove all elements in the List that match a certain predicate expression. It is often easiest to use the lambda expression syntax with the RemoveAll method, which can reduce line count. ","RemoveAll ","removeall","RemoveRange."," Here we see the RemoveRange method, which can remove elements in a certain series of indexes. One useful way to use this method is to remove the first or last several elements at once. ","Next: ","We remove all elements except the last two. The code is robust because it uses Math.Max to avoid negative parameters.","Math.Max ","math-max","C# program that uses RemoveRange method","\n\nusing System;\nusing System.Collections.Generic;\n\nclass Program\n{\n static void Main()\n {\n List<int> list = new List<int>();\n list.Add(1);\n list.Add(2);\n list.Add(3);\n list.Add(4);\n list.Add(5);","\n\n // Remove all except last 2\n ","int remove = Math.Max(0, list.Count - 2);\n list.","RemoveRange","(0, remove);\n\n foreach (int i in list)\n {\n Console.Write(i);\n }\n }\n}\n\n","Output","\n\n45","Next,"," another useful way to call RemoveRange is to remove the first N elements in your List. We also use Math.Min here to avoid arguments that are too large and would raise an exception. ","Math.Min ","math-min","C# program that uses RemoveRange on first elements","\n\nusing System;\nusing System.Collections.Generic;\n\nclass Program\n{\n static void Main()\n {\n List<int> list = new List<int>();\n list.Add(1);\n list.Add(2);\n list.Add(3);\n list.Add(4);\n list.Add(5);","\n\n // Remove first 2 elements\n ","int remove = Math.Min(list.Count, 2);\n list.","RemoveRange","(0, remove);\n\n foreach (int i in list)\n {\n Console.Write(i);\n }\n }\n}\n\n","Output","\n\n345","Summary."," We saw several useful examples of using the List type's Remove, RemoveAt, RemoveAll and RemoveRange methods. We found out how to remove the first N and last N elements, and also reviewed the algorithmic complexity of the methods. ","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","br","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"],"url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANwAAAA0CAMAAADWk6ZcAAAAMFBMVEX////Mx4SzsI61tbX585nn5+b798D9+9/h23jR0dD4+PWYmJjq43b48X3y62/48Gv13fU9AAAD0klEQVR4Xt3Z647iMAwF4Nzvad//bZcIzZr6hDrsFGnh/MxQ7C+pOgbUiIr7NO6WEKL63CAOkeGLcJgevgiHcZ/o8vUWs3+VDnGy7ptxe/hmXP9QXEppe3RsaYTr4kfiRg6UdF/TcF9+Cw517rtwNX02zlqLjFbh6FZwMYR/mkdD4BdFWBLqjkzLWsC5ku84s44Lbqf0p77oHnKn9cOQRysj6ENZfxyDi4xTKi/iqB8WF+VRLwwsu2assNe8VDcUGafsKo76YQkizql4bK5HvoI6ua6My/C4DML2QZyE20HiCP+LuiLOD9y2MqEEeR6lp5feX05/va6EK7WatrKBcdsXdX5p1Fs9OidP+XNcjNFrva/Y/Cb0BbhLjs5bYT8AB5FtWQt9yTg5EcvWJlyyjuvxma0eDq5pU41+XAoiLmHp1oSdzWwybAZmxUXcpvPz6U1PJhuzPcVBqWYAjEsBbLVBXaaLS7hGNog9FjE4tXUBp7EtI021fuDhTRg4AE66K3EHWQ2c2iY4kGjhGDrfUrwCrxFw0phQ2A6G4vOwHcjxHIdTXsOlnW0pOyQHk/BI/N3TMrPNciNphOEoluM8SJxHHLwH623ocHWOa8bQMw+ahNsDs45TGXDKnuL85IwKfW4TcWlwTROGoEKXXYvLpzj8axmL67ilr1D8JTiPOH+Gm5+rR5w7xVVhxs+wAbOUq3F1dnLqZdwG9+XruK4uxvn5c9Eu49zwEpgqxFsKNcpwicc5F96Cq3NcW3mguPwEBwO3mQwoFE+qi3EbZ1giS7iygsv8/TTHlbfh8FuR5X/iTs1wEXDsTtiONG3U23CaO+zy+OUQPMg+MByMhZait32Lb8CpyTDiBmL+ScLCDyHdtaSfjR8Rnlv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aKdIy3FG9h1Y8US9m8fuA6LI+VHsVe7sswSv6pPu2axnEz/dmW5kQ3Qu31AbuvNQbspvbKvXEfsB/DszU579cyn7Gqw3Aw7tpc0tvF927f1CEuuV7syMeOLs0e39kBTSfLbXu0WHyUqPfNdR3aZYA0txUofwV1kl13H7ax/8IlrGKg6u23XU1yVSlrr0+2soWknM27+v5BrO+9LdrFU2rQzLtso1nYB75/XQ8z5S/YSTnFV3HHg4ozM/E27nLivaq7r0Z7FC5NwC3vjsgI7sheLOEa8MbEd2ZOMz63DR+jIToQiXbbLE5/hSntByHbK0gDZLp+38rwLOIDP2pGo1mOmVe7lMT2TAWN+XcjRnmr75uCwcYMcMyIzYs7x9duDRf4Jc6LSt+ztQEmF1e1tr/Zx886/C7shuaDkoCN7gNaKkXMd2cf2TMpBP3ZJY4JqN5pe7GbmwEoOajsQ7N/MzZuMMQBu18NUmocEda/G1fXtP47JaK+yvhpRAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIwAAAB4BAMAAADS7QcCAAAAGFBMVEX////0qHKhmbLDvs74yKT4xaH4yqr2tYUVUWgbAAAAfUlEQVR42u3XsQmAMBgF4V9xAAOCfVxAdAEdQtwg1il0fsHS8IpIsLob4OvPnKy1V7WXWb+J9pQZZpG3zkRNyowmmmBgYGBgYGBgYGBgYGA+Mn0QHTl3V+o1iYieKifLYq4gilnMYqIVBgYGBgYGBgYGBgYG5gfm3ESx0GvePshxrr29cIIAAAAASUVORK5CYII=)"]

["url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANIAAABFCAMAAAA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