Skip to main content

Posts

Showing posts with the label generic list

c# - How to add an item to the beginning of a list

Add an item to the beginning of a List The List class represents a strongly typed list of objects. This class provides methods to search, sort, and manipulate lists. The List class is the generic equivalent of the ArrayList class. The list is not guaranteed to be sorted. Elements of a list can be accessed using an integer index and the indexes are zero-based. The list accepts null as a valid value for reference types and allows duplicate elements. The following .net c# tutorial code demonstrates how we can add an item to the beginning of a List instance. But there is no direct method to add an item to the beginning of a list. So we have to apply a trick to achieve it. We will insert an item at the zero index position of a list that will add the item to the list beginning. Here we will use the List class Insert() method to add an item to the list first. The List class Insert(Int32, T) method inserts an element into the List at the specified index. The ...

c# - How to sort a list

Sort a List The List class represents a strongly typed list of objects. This class provides methods to search, sort, and manipulate lists. The List class is the generic equivalent of the ArrayList class. The list is not guaranteed to be sorted. Elements of a list can be accessed using an integer index and the indexes are zero-based. The list accepts null as a valid value for reference types and allows duplicate elements. The following .net c# tutorial code demonstrates how we can sort a list instance. In this tutorial, we will sort list elements alphabetically (ascending order) using a specified comparer. Here we will use the List class Sort() method to sort list items. The List class Sort() method sorts the elements or a portion of the elements in the list using either the specified or default comparer implementation or a provided comparison delegate to compare list elements. The List class Sort(IComparer<T>) method overload sorts the elements in t...

c# - How to sort list items in ascending order

Sort list items in ascending order The List class represents a strongly typed list of objects. This class provides methods to search, sort, and manipulate lists. The List class is the generic equivalent of the ArrayList class. The list is not guaranteed to be sorted. Elements of a list can be accessed using an integer index and the indexes are zero-based. The list accepts null as a valid value for reference types and allows duplicate elements. The following .net c# tutorial code demonstrates how we can sort list items in ascending order. Here we will use the Enumerable class OrderBy() method to sort list items. And the Enumerable class ToList() method to convert the sorted sequence to a list object. The Enumerable class OrderBy() method sorts the elements of a sequence in ascending order. This method returns an ascending sorted sequence. Then we will call the Enumerable ToList() method to convert the returned sequence to a list. The Enumerable ToList() me...

c# - How to copy items from list to list without foreach

Copy list items to another list without foreach loop The List class represents a strongly typed list of objects. This class provides methods to search, sort, and manipulate lists. The List class is the generic equivalent of the ArrayList class. The list is not guaranteed to be sorted. Elements of a list can be accessed using an integer index and the indexes are zero-based. The list accepts null as a valid value for reference types and allows duplicate elements. The following .net c# tutorial code demonstrates how we can copy list items to another list without the foreach loop. Here we will use the List class specified constructor to create a list instance by copying items from another list. The List class List<T>(IEnumerable<T>) constructor initializes a new instance of the List class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. The List(System.Collect...

c# - How to clone a list

Clone a list The List class represents a strongly typed list of objects. This class provides methods to search, sort, and manipulate lists. The List class is the generic equivalent of the ArrayList class. The list is not guaranteed to be sorted. Elements of a list can be accessed using an integer index and the indexes are zero-based. The list accepts null as a valid value for reference types and allows duplicate elements. The following .net c# tutorial code demonstrates how we can clone a list instance. In this .net c# tutorial code, we used the List class specified constructor to create a clone of a list instance. The List class List<T>(IEnumerable<T>) constructor initializes a new instance of the List class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied. So, using this constructor the .net c# developers can create a clone of a list object. The cloned list w...

c# - How to get a range of elements from a list

Generic List GetRange() Method .Net framework generic list GetRange() method allow us to create a shallow copy of a range of elements in the source List<T>.the GetRange() method is exists under System.Collections.Generic namespace. this method require to pass two parameters, named 'index' and 'count'. both 'index' and 'count' parameters data type is System.Int32. the 'index' represent the zero based List<T> index at which the range startsand the 'count' represent the number of elements in the range. this method return value type is System.Collections.Generic.List<T>. this returned List<T> is a shallow copy of a range of elements in the sourceList<T>. this method throw ArgumentOutOfRangeException exception if the 'index' is less than zero (0) or 'count' is less than 0. this method also throwArgumentException exception if the 'index' and 'count...