Found 34494 Articles for Programming

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

karthikeya Boyini
Updated on 22-Jun-2020 14:07:28

2K+ Views

Use the InsertRange() method to insert a list in between the existing lists in C#. Through this, you can easily add more than one element to the existing list.Let us first set a list −List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50);Now, let us set an array. The elements of this array are what we will add to the above list −int[] arr2 = new int[4]; arr2[0] = 60; arr2[1] = 70; arr2[2] = 80; arr2[3] = 90;We will add the above elements to the list −arr1.InsertRange(5, arr2);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public ... Read More

Insert an element at second position in a C# List

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 is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       List val = new List ();       // list of strings       val.Add("water");       val.Add("food");       val.Add("air");       ... Read More

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

karthikeya Boyini
Updated on 22-Jun-2020 14:08:38

5K+ Views

Set the dictionary elements −Dictionary d = new Dictionary(); // dictionary elements d.Add(1, "One"); d.Add(2, "Two"); d.Add(3, "Three"); d.Add(4, "Four"); d.Add(5, "Five"); d.Add(6, "Six"); d.Add(7, "Seven"); d.Add(8, "Eight");To get the keys, use a list collection −List keys = new List(d.Keys);Loop through the keys and display them.Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       Dictionary d = new Dictionary();       // dictionary elements       d.Add(1, "One");       d.Add(2, "Two");       d.Add(3, "Three");       d.Add(4, "Four"); ... Read More

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

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() {       List val = new List();       // list of strings       val.Add("water");       val.Add("food");       val.Add("air");       string res = string.Join(",", val.ToArray());       Console.WriteLine(res);    } }Outputwater,food,air

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

karthikeya Boyini
Updated on 22-Jun-2020 14:09:40

407 Views

Declare an array −int[] arr = { 10, 15, 5, 20};Now to get the smallest element from an array, use Min() method with lambda expressions −arr.Min());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 10, 15, 5, 20};       Console.WriteLine(arr.Min(element => Math.Abs(element)));    } }Output5

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

karthikeya Boyini
Updated on 22-Jun-2020 14:10:03

709 Views

Set a list and add elements −List val = new List(); // integer elements val.Add(35); val.Add(55); val.Add(68);Let’s say now we need to find the index of element 68. For that, use the IndexOf() method −int index = val.IndexOf(68);Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       List val = new List();       // integer elements       val.Add(35);       val.Add(55);       val.Add(68);       // finding the index of element 68       int index = val.IndexOf(68);       Console.WriteLine(index);    } }Output2

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

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

324 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 class Demo {    public static void Main() {       int[] val = new int[5];       // integer elements       val[0] = 15;       val[1] = 25;       val[2] = 40;       val[3] = 58;       val[4] = 70;       List myList = new List(val);       Console.WriteLine("Elements...");       foreach(int res in myList) {          Console.WriteLine(res);       }       // count integer elements       Console.WriteLine("Number of elements: "+myList.Count);    } }OutputElements... 15 25 40 58 70 Number of elements: 5

Clear a list in C#

karthikeya Boyini
Updated on 22-Jun-2020 13:58:40

367 Views

Firstly, set a list −List myList = new List(); myList.Add(45); myList.Add(77);Now, to clear the above list, use Clear() −myList.Clear();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       List myList = new List();       myList.Add(45);       myList.Add(77);       Console.WriteLine("Elements: "+myList.Count);       myList.Clear();       Console.WriteLine("Elements after using clear: "+myList.Count);    } }OutputElements: 2 Elements after using clear: 0

TrueForAll() method in C# Arrays

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 };       // checking whether all the array element are more than one or not       bool result = Array.TrueForAll(val, res => res > 1);       Console.WriteLine(result);    } }OutputTrueWith TrueForAll() method in arrays, you can check every element for a condition.Let us see an example −Example Live Demousing System; ... Read More

C# program to find the last matching element in an array

karthikeya Boyini
Updated on 22-Jun-2020 13:59:37

295 Views

To find the last matching element, use the Array.LastIndexOf method. Returns -1 if the element isn’t present in the integer array.The following is the array −int[] val = { 97, 45, 76, 21, 89, 45 };Now, let’s say you need to search the last Index of element 45. For that, use Array.LastIndexOf() method −int res = Array.LastIndexOf(val, 45);The following is an example −Example Live Demousing System; using System.Text; public class Demo {    public static void Main() {       int[] val = { 97, 45, 76, 21, 89, 45 };       // last Index of element 45       int res = Array.LastIndexOf(val, 45);       // Display the index       Console.WriteLine("Index of element 45 is = "+res);    } }OutputIndex of element 45 is = 5

Advertisements