Check If a HashSet Contains the Specified Element in C#

AmitDiwan
Updated on 04-Dec-2019 06:20:37

781 Views

To check if a HashSet contains the specified element, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(){       HashSet set1 = new HashSet();       set1.Add(25);       set1.Add(50);       set1.Add(75);       set1.Add(100);       set1.Add(125);       set1.Add(150);       Console.WriteLine("Elements in HashSet1");       foreach(int val in set1){          Console.WriteLine(val);       }       HashSet set2 = new HashSet();       set2.Add(30);       set2.Add(60);     ... Read More

Check If an Array is Read-Only in C#

AmitDiwan
Updated on 04-Dec-2019 06:17:11

235 Views

To check if an array is read-only or not, try the below code −Example Live Demousing System; public class Demo {    public static void Main(){       string[] products = new string[] { };       Console.WriteLine("One or more planets begin with 'E'? = {0}",          Array.Exists(products, ele => ele.StartsWith("E")));       Console.WriteLine("Is the array having fixed size? = " + products.IsFixedSize);       Console.WriteLine("Is the array read only? = " + products.IsReadOnly);    } }OutputThis will produce the following code −One or more planets begin with 'E'? = False Is the array ... Read More

Get Enumerator to Iterate Through Dictionary in C#

AmitDiwan
Updated on 04-Dec-2019 06:06:52

544 Views

To get an enumerator that iterates through the Dictionary, the code is as follows −Example Live Demousing System; using System.Collections; using System.Collections.Generic; public class Demo {    public static void Main(){       Dictionary dict = new Dictionary();       dict.Add(100, "Laptop");       dict.Add(150, "Desktop");       dict.Add(200, "Earphone");       dict.Add(300, "Tablet");       dict.Add(500, "Speakers");       dict.Add(750, "HardDisk");       dict.Add(1000, "SSD");       IDictionaryEnumerator demoEnum = dict.GetEnumerator();       Console.WriteLine("Enumerator iterating key-value pairs...");       while (demoEnum.MoveNext())       Console.WriteLine("Key = " + demoEnum.Key ... Read More

Get Enumerator to Iterate Through StringCollection in C#

AmitDiwan
Updated on 04-Dec-2019 06:02:56

166 Views

To get an enumerator that iterates through StringCollection, the code is as follows −Example Live Demousing System; using System.Collections.Specialized; public class Demo {    public static void Main(){       StringCollection stringCol = new StringCollection();       String[] arr = new String[] { "100", "200", "300", "400", "500" };       Console.WriteLine("Array elements...");       foreach (string res in arr){          Console.WriteLine(res);       }       stringCol.AddRange(arr);       Console.WriteLine("Does the specified string is in the StringCollection? = "+stringCol.Contains("800"));       Console.WriteLine("Total number of elements = "+stringCol.Count);     ... Read More

Removing All Nodes from Linked List in C#

AmitDiwan
Updated on 04-Dec-2019 05:58:13

215 Views

To remove all nodes from LinkedList, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       int [] num = {10, 20, 30, 40, 50};       LinkedList list = new LinkedList(num);       Console.WriteLine("LinkedList nodes...");       foreach (var n in list) {          Console.WriteLine(n);       }       list.Clear();       Console.WriteLine("LinkedList is empty now!");       foreach (var n in list) {          Console.WriteLine(n);       }    } ... Read More

Remove All Entries from StringDictionary in C#

AmitDiwan
Updated on 04-Dec-2019 05:52:40

105 Views

To remove all entries from the StringDictionary, the code is as follows −Example Live Demousing System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main(){       StringDictionary strDict1 = new StringDictionary();       strDict1.Add("A", "John");       strDict1.Add("B", "Andy");       strDict1.Add("C", "Tim");       strDict1.Add("D", "Ryan");       strDict1.Add("E", "Kevin");       strDict1.Add("F", "Katie");       strDict1.Add("G", "Brad");       Console.WriteLine("StringDictionary1 elements...");       foreach(DictionaryEntry d in strDict1){          Console.WriteLine(d.Key + " " + d.Value);       }       ... Read More

Check If an Array Has Fixed Size in C#

AmitDiwan
Updated on 04-Dec-2019 05:48:26

151 Views

To check if an array has fixed size or not, try the below code −Example Live Demousing System; public class Demo {    public static void Main(){       string[] products = new string[] { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "Furniture" };       Console.WriteLine("Products list...");       foreach(string s in products){          Console.WriteLine(s);       }       Console.WriteLine("Is the products Accessories in the array? = {0}",          Array.Exists(products, ele => ele == "Accessories"));       Console.WriteLine("Is the products Stationery in the array? = {0}",       ... Read More

Check If an Array Contains Elements Matching Specified Conditions in C#

AmitDiwan
Updated on 04-Dec-2019 05:44:00

468 Views

To check if an array contains the elements that match the specific conditions, we can use the StartsWith() method in C# −Example Live Demousing System; public class Demo {    public static void Main(){       string[] products = { "Electronics", "Accessories", "Clothing", "Toys", "Clothing", "Furniture" };       Console.WriteLine("Products list...");       foreach(string s in products){          Console.WriteLine(s);       }       Console.WriteLine("One or more products begin with the letter 'C'? = {0}",       Array.Exists(products, ele => ele.StartsWith("C")));       Console.WriteLine("One or more planets begin with 'D'? = ... Read More

Extract Subset of Key-Value Pairs from Python Dictionary Object

Jayashree
Updated on 04-Dec-2019 05:31:43

3K+ Views

Use dictionary comprehension technique.We have dictionary object having name and percentage of students>>> marks = {    'Ravi': 45.23,    'Amar': 62.78,    'Ishan': 20.55,    'Hema': 67.20,    'Balu': 90.75 }To obtain dictionary of name and marks of students with percentage>50>>> passed = { key:value for key, value in marks.items() if value > 50 } >>> passed {'Amar': 62.78, 'Hema': 67.2, 'Balu': 90.75}To obtain subset of given names>>> names = { 'Amar', 'Hema', 'Balu' } >>> lst = { key:value for key,value in marks.items() if key in names} >>> lst {'Amar': 62.78, 'Hema': 67.2, 'Balu': 90.75}

Check If a Value Is in LinkedList in C#

AmitDiwan
Updated on 04-Dec-2019 05:23:50

117 Views

To check if a value is in LinkedList, try the below code −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(){       LinkedList linkedList = new LinkedList();       linkedList.AddLast(25);       linkedList.AddLast(50);       linkedList.AddLast(100);       linkedList.AddLast(200);       linkedList.AddLast(400);       linkedList.AddLast(500);       linkedList.AddLast(550);       linkedList.AddLast(600);       linkedList.AddLast(800);       linkedList.AddLast(1200);       Console.WriteLine("Count of nodes = " + linkedList.Count);       foreach(int val in linkedList){          Console.WriteLine(val);       } ... Read More

Advertisements