Csharp Articles

Page 135 of 196

C# Program to convert integer array to string array

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 2K+ Views

Converting an integer array to a string array is a common task in C# programming. The most efficient approach is using the Array.ConvertAll() method, which applies a conversion function to each element in the source array and returns a new array of the target type. Syntax Following is the syntax for Array.ConvertAll() method − string[] result = Array.ConvertAll(sourceArray, converter); Parameters sourceArray − The source integer array to convert converter − A lambda expression or method that converts each element Return Value Returns a new string ...

Read More

C# program to check if a value is in an array

Samual Sam
Samual Sam
Updated on 17-Mar-2026 1K+ Views

Use the Array.Exists method to check if a value is in an array or not. This method returns a boolean value indicating whether the specified element exists in the array. Syntax Following is the syntax for Array.Exists method − public static bool Exists(T[] array, Predicate match) Parameters array − The one-dimensional array to search. match − A predicate that defines the conditions of the element to search for. Return Value Returns true if the array contains an element that matches the conditions defined by the ...

Read More

Clear a list in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 513 Views

To clear a list in C#, you use the Clear() method which removes all elements from the list. This method is part of the List class and provides an efficient way to empty a list without recreating it. Syntax Following is the syntax for the Clear()

Read More

C# program to create a List with elements from an array

Samual Sam
Samual Sam
Updated on 17-Mar-2026 511 Views

In C#, you can easily create a List with elements from an existing array. The List constructor accepts an IEnumerable parameter, which allows you to initialize the list directly with array elements. Syntax Following is the syntax to create a list from an array − List listName = new List(arrayName); You can also use the ToList() extension method − List listName = arrayName.ToList(); Using List Constructor The most straightforward way is to pass the array directly to the List constructor − using System; using System.Collections.Generic; public ...

Read More

C# program to find the index of an element in a List

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 1K+ Views

In C#, you can find the index of an element in a List using the IndexOf() method. This method returns the zero-based index of the first occurrence of the specified element, or -1 if the element is not found. Syntax Following is the syntax for the IndexOf() method − int IndexOf(T item) int IndexOf(T item, int startIndex) int IndexOf(T item, int startIndex, int count) Parameters item: The element to locate in the list. startIndex: The zero-based starting index of the search (optional). count: The number of elements to search (optional). ...

Read More

C# Program to find the smallest element from an array using Lambda Expressions

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 620 Views

Lambda expressions in C# provide a concise way to write anonymous functions. When combined with LINQ methods like Min(), they offer powerful tools for array operations. This article demonstrates how to find the smallest element from an array using lambda expressions. Syntax The basic syntax for using Min() with lambda expressions − array.Min() // finds minimum value directly array.Min(element => expression) // finds minimum based on expression Using Min() Without Lambda Expression For ...

Read More

C# program to get the List of keys from a Dictionary

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 7K+ Views

In C#, you can extract all the keys from a Dictionary as a List using the Keys property. This is useful when you need to work with dictionary keys as a separate collection or perform operations like sorting, filtering, or iteration. Syntax Following is the syntax to get keys from a Dictionary − Dictionary dictionary = new Dictionary(); List keys = new List(dictionary.Keys); You can also use LINQ to convert keys to a List − List keys = dictionary.Keys.ToList(); Using Dictionary.Keys Property The Keys property returns a collection of ...

Read More

Insert more than one element at once in a C# List

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 3K+ Views

The InsertRange() method in C# allows you to insert multiple elements into a List at a specified position. This method is useful when you need to add a collection of elements at once rather than adding them one by one. Syntax Following is the syntax for the InsertRange() method − public void InsertRange(int index, IEnumerable collection) Parameters index − The zero-based index at which the new elements should be inserted. collection − The collection whose elements should be inserted into the List. The collection itself cannot be null, but ...

Read More

Get the range of elements in a C# list

Samual Sam
Samual Sam
Updated on 17-Mar-2026 3K+ Views

The GetRange() method in C# is used to extract a subset of elements from a List. This method returns a new list containing the specified range of elements from the original list, without modifying the original list. Syntax Following is the syntax for the GetRange() method − public List GetRange(int index, int count) Parameters index − The zero-based starting index of the range to extract. count − The number of elements to extract from the starting index. Return Value Returns a new List containing the specified range of elements. ...

Read More

Remove duplicates from a List in C#

karthikeya Boyini
karthikeya Boyini
Updated on 17-Mar-2026 7K+ Views

Removing duplicates from a List in C# is a common task when working with collections. There are several approaches to accomplish this, with Distinct() being the most straightforward method. The Distinct() method uses LINQ to filter out duplicate elements based on their default equality comparison. Syntax Following is the syntax for using Distinct() to remove duplicates − List uniqueList = originalList.Distinct().ToList(); For custom equality comparison − List uniqueList = originalList.Distinct(comparer).ToList(); Using Distinct() Method The Distinct() method from LINQ is the simplest way to remove duplicates from a list. It ...

Read More
Showing 1341–1350 of 1,951 articles
« Prev 1 133 134 135 136 137 196 Next »
Advertisements