Samual Sam has Published 2310 Articles

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

Samual Sam

Samual Sam

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

437 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

609 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

3K+ 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

3K+ 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

112 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

1K+ 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

144 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

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

Samual Sam

Samual Sam

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

457 Views

Set an array −int[] val = new int[5]; // integer elements val[0] = 15; val[1] = 25; val[2] = 40; val[3] = 58; val[4] = 70;Now set a list and add array in it −List myList = new List(val);The following is the code −Example Live Demousing System; using System.Collections.Generic; public ... Read More

Declare char arrays in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 13:46:46

7K+ Views

Declare a char array and set the size −char[] arr = new char[5];Now set the elements −arr[0] = 'h'; arr[1] = 'a'; arr[2] = 'n'; arr[3] = 'k'; arr[4] = 's';Let us see the complete code now to declare, initialize and display char arrays in C# −Example Live Demousing System; ... Read More

Advertisements