Found 34488 Articles for Programming

Empty List in C#

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 Demo {    public static void Main() {       List myList = new List();       // returns false i.e. an empty list (not a null list)       Console.WriteLine(myList == null);    } }OutputFalse

C# Program to Subtract Two TimeSpan

karthikeya Boyini
Updated on 22-Jun-2020 14:04:46

350 Views

Firstly, set two TimeSpans −TimeSpan t1 = TimeSpan.FromMinutes(2); TimeSpan t2 = TimeSpan.FromMinutes(1);To add it, use the Subtract() method −imeSpan res = t1.Subtract(t2);Here is the complete code −Example Live Demousing System; using System.Linq; public class Demo {    public static void Main() {       TimeSpan t1 = TimeSpan.FromMinutes(2);       TimeSpan t2 = TimeSpan.FromMinutes(1);       Console.WriteLine("First TimeSpan: "+t1);       Console.WriteLine("Second TimeSpan: "+t2);       // Subtracting       TimeSpan res = t1.Subtract(t2);       Console.WriteLine("Resultant TimeSpan: "+res);    } }OutputFirst TimeSpan: 00:02:00 Second TimeSpan: 00:01:00 Resultant TimeSpan: 00:01:00

Null List in C#

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; using System.Linq; public class Demo {    public static void Main() {       List myList = null;       // checking for null       Console.WriteLine(myList == null);    } }OutputTrue

Remove duplicates from a List in C#

karthikeya Boyini
Updated on 22-Jun-2020 14:06:01

6K+ Views

Use the Distinct() method to remove duplicates from a list in C#.Firstly, add a new list −List arr1 = new List(); arr1.Add(10); arr1.Add(20); arr1.Add(30); arr1.Add(40); arr1.Add(50); arr1.Add(30); arr1.Add(40); arr1.Add(50);To remove duplicate elements, use the Distinct() method as shown below −List distinct = arr1.Distinct().ToList();Here is the complete code −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List arr1 = new List();       arr1.Add(10);       arr1.Add(20);       arr1.Add(30);       arr1.Add(40);       arr1.Add(50);       arr1.Add(30);       arr1.Add(40); ... Read More

Get the range of elements in a C# list

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 −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       List arr1 = new List();       arr1.Add(10);       arr1.Add(20);       arr1.Add(30);       arr1.Add(40);       arr1.Add(50);       Console.WriteLine("Initial List ...");   ... Read More

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

334 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

409 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

Advertisements