Skip to main content

Posts

Showing posts from September, 2013

c# - How to intersect two arrays

Intersect two Arrays The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can intersect two array instances. But there is no built-in method in the Array class two intersect two Array objects. So in this .net c# tutorial code, we will use Enumerable Intersect() method to intersect two Array instances. Here we will intersect two integer-type Array instances. The Enumerable Intersect() method produces the set intersection of two sequences. This method is located under System.Linq namespace. The Enumerable Intersect<TSource>(IEnumerable<TSource>, IEnum...

c# - How to get value of a specified element of an array

Array get value The following asp.net c# example code demonstrate us how can we get a specified element value froman array object programmatically at run time in an asp.net application. .Net framework's Array Class Array.GetValue()method get the value of the specified element in the current array. Array.GetValue(Int32) overloaded method allow us to get the value at the specified position in the one-dimensional array.In this example, we uses this overloaded member. GetValue(Int32) method has a required parameter named 'index' which valuedata type is System.Int32. This parameter value contains a 32-bit integer that represent the position of the array elementto get. Array.GetValue(Int32) method return a System.Object type value. Method return the element value at the specified positionin the one dimensional array. So, to get a specified element value of an one-dimensional array by index number, we can call the GetValue(Int32) meth...

c# - How to get a range of elements from an array

Get a range of elements from an Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can get a range of elements from an Array instance. That means we will get a specified number of elements from an Array instance and create a new instance of an Array object by the returned elements. Here we will copy the specified elements from an Array object and create a new instance of an Array instance by copied elements. In this .net c# tutorial code we used the Array Copy() method to copy some elements from the source Array and create a new instance of an Array. ...

c# - How to filter an array elements

Array filter The following asp.net c# example code demonstrate us how can we filter an array elements depends on specific criteriaand get a new array object with filtered elements programmatically at run time in an asp.net application. .Net framework'sArray Class Array.FindAll() method retrieve all the elements that match the conditions defined by the specified predicate. Arry.FindAll<T>() method type parameter name is 'T' which represent the type of the elements of the array.FindAll() method has two required parameters named 'array' and 'match'. The 'array' parameter represent a one-dimensional arrayto search. And the 'match' parameter represent the Predicate<T> that defines the conditions of the elements to search for. FindAll() method return an array object which contains all the elements that match the conditions. Finally, we can filter anarray and get a new array object with filtered e...

c# - Find index of last element from an array that match the conditions

Array find last index The following asp.net c# example code demonstrate us, how can we get index of last occurrence of an element within an arraywhich elements are matches the search condition, programmatically at run time in an asp.net application. .Net framework's Array Class Array.FindLastIndex() method searches for an element that matches the conditiondefined by a specified predicate, return the zero-based index of the last occurrence within an array or a portion of array.Array.FindLastIndex() method has few overloads. ArrayFindLastIndex<T>(T[], Predicate<T>) overloaded method searches for an element that matches the conditions defined by thespecified predicate, and returns the zero-based index of the last occurrence within the entire array. ArrayFindLastIndex(T[], Predicate<T>) method has two required parameters named 'array' and 'match'. The 'array' parameterrepresent the one-dimensional,...

c# - How to get the last element of an array

Get the last element of an Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can get the last element of an Array. But there is no built-in method in the Array class to get the last element from an Array object. So in this .net c# tutorial code example, we will use the Enumerable Last() method to get the Array last element. The Enumerable Last() method returns the last element of a sequence. The Enumerable Last() method throws ArgumentNullException if the source is null. It also throws InvalidOperationException if the source sequence is empty. The ...

c# - How to get the first element of an array

Get the first element of an Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can get the first element of an Array instance. But there is no direct built-in method in the Array class to get the first element from an Array object. So, in this .net c# tutorial code we will use the Enumerable class First() method to get the first element of an Array instance. The Enumerable First() method returns the first element of a sequence. The Enumerable First<TSource>(IEnumerable<TSource>) method overload returns the first element of a sequence. Here the s...

c# - How to fill an array with a single value

Fill an Array with a single value The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can fill an Array instance with a single value. That means we will set the same value for an Array object’s all elements. But there is no built-in method in the Array class to fill an Array instance with the same value. So in this .net c# tutorial code example, we will use the Enumerable Repeat() and ToArray() methods to fill an Array object with a single value. The Enumerable Repeat() method generates a sequence that contains one repeated value. The Enumerable Repeat() method ha...

c# - How to find all elements from an array that match the conditions

Find all elements from an Array that match conditions The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can find all elements from an Array instance that match conditions. That means we will get all elements that satisfy the search criteria from an Array object. And also create an Array instance by using the returned elements. In this .net c# tutorial code, we will use the Array class FindAll() method to find all elements from an Array that match specified conditions. The Array FindAll() method retrieves all the elements that match the conditions defined by t...

c# - How to find an element from an array

Find an element from an Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can find an element from an Array instance. That means we will find the first element from an Array object which meet the conditions. In this .net c# tutorial code, we used Array Find() method to find an element from an Array instance. The Array Find() method searches for an element that matches the conditions defined by the specified predicate and returns the first occurrence within the entire Array. The Array Find<T> (T[] array, Predicate<T> match) method has two parame...

c# - How to make array except

Array Except The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can get the difference between two Array instances. Here we will compare two Array instances and get only the elements from the first Array that do not exist in the second Array instance. In this .net c# example code, we will use Enumerable Except() method to get the difference between two Array instances. The Enumerable Except() method produces the set difference of two sequences. This method exists in the System.Linq namespace. The set difference between the two sets is defined as the members of ...

c# - How to check whether an element exists in an array

Array exists The following asp.net c# example code demonstrate us how can we determine whether specified elements are exists in anarray based on search criteria. .Net framework's Array Class Array.Exists<T>() method allow us to determine whetherthe specified array contains one or more elements that match the conditions defined by the specified predicate. Array.Exists<T>() method type parameter is 't' which represent the type of the elements of the array. This method hastwo required parameters named 'array' and 'match'. The 'array' parameter represent the one-dimensional, zero-based index array tosearch. The 'match' parameter value represent the Predicate<T> that defines the conditions of the elements to search for. Array.Exists() method return a Boolean value. It return 'true', if array contains value that match the condition; otherwiseit return 'false'. In this ...

c# - How to delete an element from an array

Delete an element from an Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can delete an element from an Array. That means we will remove/delete an element from an Array instance and reduce the Array length and reallocate the Array elements. But there is no direct method in the Array class to delete an element from an Array object. So we have to perform a series of tasks to delete an element from the Array object. In this .net c# example code, we will use the Array class Resize() method, Enumerable class ToList() method and List class RemoveAt() method to...

c# - How to perform binary search on an array

Perform a binary search on an Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can perform a binary search on an Array instance. Binary search on an Array instance is worked on a sorted Array. So, we have to sort an Array instance first, and then we have to perform the binary search on the sorted Array instance. In this .net c# tutorial code, we used Array Sort() and BinarySearch() methods to perform a binary search on an Array instance. The Array Sort() method sorts the elements in a one-dimensional array. The Array BinarySearch() method searches a one-d...

c# - How to convert a string array to a comma separated string

Convert a String Array to a comma-separated String The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can convert a String Array instance to a comma-separated String object. That means we will get a String instance from the elements of a String Array where the String instance contains the Array elements separated by comma delimiter. Here we will join the String Array elements with the comma separator and build an instance of the String object. In this .net c# example code, we will use the String class Join() method. The String Join(String, String[]) method ove...

c# - How to convert a string array to a string

Convert a String Array to a String The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can convert a String Array instance to a String object. That means we will get a String instance from the elements of a String Array. Here we will join the String Array elements with a separator and build an instance of the String object. In this .net c# example code, we will use the String class Join() method. The String Join() method concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. ...

c# - How to convert a decimal array to a double array

Convert a Decimal Array to a Double Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can convert a Decimal Array to a Double Array. But there is no direct method to convert a Decimal Array object to a Double Array instance. So, we have to perform some tasks to get a Double Array from Decimal Array elements. The .net c# developers have to initialize an empty instance of a Double Array at the beginning. While initializing the Double Array instance, we will set the Double Array size exactly the same as the Decimal Array size. Next, we have to loop through...

c# - How to convert a decimal array to a string array

Convert a Decimal Array to a String Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can convert a Decimal Array to a String Array. But there is no direct method in the Array class to convert a Decimal Array instance to a String Array instance. So, we have to do some tasks to get a String Array from Decimal Array elements. The .net c# developers have to initialize an empty instance of a String Array at the beginning. While initializing the String Array instance, we will set the String Array size exactly the same as the Decimal Array size. Next, we have...

c# - How to convert a decimal array to a string

Convert a Decimal Array to a String The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can convert a Decimal Array instance to a String object. That means we will get a String object from Decimal Array elements. In this .net code, we will join the Decimal Array elements with a specified separator and build an instance of the String object. Here we will use the String class Join() method. The String class Join() method concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. ...

c# - How to convert a string array to an int array

Convert a String Array to an Int Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can convert a String Array to an Int Array. That means we will convert a String Array instance to an Int Array instance where all the elements of the Srting Array are the String representations of int values. But there is no direct method to convert a String Array object to an Int Array instance. So, we have to perform some jobs to get an Int Array from String Array elements. At first, we have to initialize an empty instance of an Int Array. While initializing the Int Arr...

c# - How to convert a string array to a double array

Convert a String Array to a Double Array The Array class provides methods for creating, manipulating, searching, and sorting arrays. The Array class is not part of the System.Collections namespaces. However, it is still considered a collection because it is based on the IList interface. An element is a value in an Array. The length of an Array is the total number of elements it can contain. The Array has a fixed capacity. The following .net c# tutorial code demonstrates how we can convert a String Array to a Double Array. That means we will convert a String Array instance whose elements are Double data type to a Double Array object. But there is no direct method to convert a String Array instance to a Double Array object. So, we have to apply some actions to get a Double Array from String Array elements. In the beginning, we have to initialize an empty instance of Double Array. In the initializing time, we will set the Double Array size the s...

c# - How to convert a double array to a string

Convert double array to string The following asp.net c# example code demonstrate us how can we convert a double array to a string objectprogrammatically at run time in an asp.net application. We can not directly convert a double array to string object. To convert, a double array to a string value, first we need to convert the double array to string array by convertingeach elements data type double to string. Then we can join the converted string array elements to create a string value. .Net framework's array object work as like a collection, so we can apply Select method to an array. Using LinqSelect method we select all elements from the double array and convert each element value data type double to string.Finally, we convert the double array to string array by using Linq Select() method and ToArray() method. Next, we can join each elements of double array to create a new string object. We can use a separator to join array elements...

c# - How to add a new item in an existing array

Add new item in existing array The following asp.net c# example code demonstrate us how can we add an item/element to an existingarray programmatically at run time in an asp.net application. .Net framework's array class has no direct built inmethod or property to add or append an element with value to array elements collection. .Net framework's array object is a fixed size elements collection. so, if we want to add an item to an existingarray object, then fist we need to resize array object to allocate available space for new element. Array.resize() methodallow us to change the number of elements of a one-dimensional array to the specified new size. To add a new element to an array object we can set array new size as Array.Length+1. Now, the last element of the array isour newly added empty element. We can set a value for this newly added element as this way Array[Array.Length-1]="value".array.Length-1 indicate the last eleme...