Samual Sam has Published 2492 Articles

Find a specific element in a C# List

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:29:21

164 Views

Set a list −List myList = new List() {    5,    10,    17,    19,    23,    33 };Let us say you need to find an element that is divisible by 2. For that, use the Find() method −int val = myList.Find(item => item % 2 == ... Read More

Empty List in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:14:20

8K+ Views

Set a list that has zero elements −List myList = new List();Now check whether the list is empty or null −Console.WriteLine(myList == null);Above, returns “False” i.e. the list is not null - the list is empty.Let us see the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class ... Read More

C# program to convert several strings into a single comma-delimited string

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:09:08

333 Views

Set the strings −List val = new List(); // list of strings val.Add("water"); val.Add("food"); val.Add("air");Use Join method to convert several strings into a single comma-delimited string −string.Join(", ", val.ToArray());Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {   ... Read More

Insert an element at second position in a C# List

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:08:04

451 Views

Here is our list −List val = new List (); // list of strings val.Add("water"); val.Add("food"); val.Add("air");Use the Insert() method to insert an element in the list. With that, also set where you want to add it. We have set the new text at position 1st −val.Insert(1, "shelter");The following ... Read More

Get the range of elements in a C# list

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:06:43

2K+ Views

Use the GetRange() method to get the range of elements −Firstly, set a list and add elements −List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50);Now, under a new list get the range of elements between index 1 and 3 −List myList = arr1.GetRange(1, 3);Here is the complete code ... Read More

Null List in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:05:11

2K+ Views

A null list exists in C#. To check whether a list is null or not, check them against the null literal. Set a null like this −List myList = null;Now, to check for null, use the equality operator −myList == null;The following is an example −Example Live Demousing System; using System.Collections.Generic; ... Read More

Merge two arrays using C# AddRange() method

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:02:58

2K+ Views

Firstly, set two arrays −int[] arr1 = { 15, 20, 27, 56 }; int[] arr2 = { 62, 69, 76, 92 };Now create a new list and use AddRange() method to merge −var myList = new List(); myList.AddRange(arr1); myList.AddRange(arr2);After that, convert the merged collection to an array −int[] arr3 = ... Read More

Array.BinarySearch Method in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 14:01:25

60 Views

Get the location of array elements using the BinarySearch method.Set a string array −string[] str = { "a", "m", "i", "t"};Now get the location of character ‘t’ using Array.BinarySearch −Array.BinarySearch(str, "t");Here is the complete code −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() { ... Read More

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

Samual Sam

Samual Sam

Updated on 22-Jun-2020 13:59:58

936 Views

Use the Array.Exists method to check if a value is in an array or not.Set a string array −string[] strArray = new string[] {"keyboard", "screen", "mouse", "charger" };Let’s say you need to find the value “keyboard” in the array. For that, use Array.Exists() −Array.Exists(strArray, ele => ele == "keyboard");It returns ... Read More

TrueForAll() method in C# Arrays

Samual Sam

Samual Sam

Updated on 22-Jun-2020 13:59:12

107 Views

With TrueForAll() method in arrays, you can check every element for a condition.Let us see an example −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       int[] val = { 97, 45, 76, 21, 89, 45 };       // ... Read More

Advertisements