Found 2628 Articles for Csharp

Intersection of two HashSets in C#

AmitDiwan
Updated on 11-Dec-2019 07:02:09

335 Views

To find the intersection of two HashSets, 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("AB");       set1.Add("CD");       set1.Add("EF");       set1.Add("AB");       set1.Add("IJ");       set1.Add("KL");       set1.Add("EF");       set1.Add("OP");       Console.WriteLine("Elements in HashSet1");       foreach(string val in set1){          Console.WriteLine(val);       }       HashSet set2 = new HashSet();       set2.Add("EF"); ... Read More

Intersection of SortedSet with a collection in C#

AmitDiwan
Updated on 11-Dec-2019 06:58:58

95 Views

To get the intersection of SortedSet with a Collection, the code is as follow −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(){       SortedSet set1 = new SortedSet();       set1.Add(100);       set1.Add(200);       set1.Add(300);       SortedSet set2 = new SortedSet();       set2.Add(450);       set2.Add(200);       set2.Add(650);       set2.Add(300);       set2.Add(800);       Console.WriteLine("Does it contain the same elements? = "+set1.SetEquals(set2));       set1.IntersectWith(set2);       Console.WriteLine("Resultant SortedSet...");       foreach(int ... Read More

Insert into OrderedDictionary with key and value at specified index in C#

AmitDiwan
Updated on 11-Dec-2019 06:53:56

204 Views

To insert into OrderedDictionary with key and value at specified index, the code is as follows −Example Live Demousing System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main(){       OrderedDictionary dict = new OrderedDictionary();       dict.Add("A", "Books");       dict.Add("B", "Electronics");       dict.Add("C", "Smart Wearables");       dict.Add("D", "Pet Supplies");       Console.WriteLine("OrderedDictionary elements...");       foreach(DictionaryEntry d in dict){          Console.WriteLine(d.Key + " " + d.Value);       }       Console.WriteLine("Count of elements in OrderedDictionary = " + dict.Count); ... Read More

Insert at the specified index in StringCollection in C#

AmitDiwan
Updated on 11-Dec-2019 06:48:14

101 Views

To insert at the specified index in StringCollection, the code is as follows −Example Live Demousing System; using System.Collections.Specialized; public class Demo {    public static void Main(){       StringCollection strCol = new StringCollection();       strCol.Add("Accessories");       strCol.Add("Books");       strCol.Add("Electronics");       Console.WriteLine("StringCollection elements...");       foreach (string res in strCol){          Console.WriteLine(res);       }       strCol.Insert(2, "Headphone");       Console.WriteLine("StringCollection elements...UPDATED");       foreach (string res in strCol){          Console.WriteLine(res);       }    } }OutputThis will ... Read More

Insert an element into Collection at specified index in C#

AmitDiwan
Updated on 11-Dec-2019 06:45:10

272 Views

To insert an element into Collection at the specified index, the code is as follows −Example Live Demousing System; using System.Collections.ObjectModel; public class Demo {    public static void Main(){       Collection col = new Collection();       col.Add("Laptop");       col.Add("Desktop");       col.Add("Notebook");       col.Add("Ultrabook");       col.Add("Tablet");       col.Add("Headphone");       col.Add("Speaker");       Console.WriteLine("Elements in Collection...");       foreach(string str in col){          Console.WriteLine(str);       }       Console.WriteLine("Element at index 3 = " + col[3]);     ... Read More

Insert a new entry in OrderedDictionary with specified key and value in C#

AmitDiwan
Updated on 11-Dec-2019 06:41:26

59 Views

To insert a new entry in OrderedDictionary with specified key and value, the code is as follows −Example Live Demousing System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main(){       OrderedDictionary dict = new OrderedDictionary();       dict.Add("1", "One");       dict.Add("2", "Two");       dict.Add("3", "Three");       dict.Add("4", "Four");       dict.Add("5", "Five");       dict.Add("6", "Six");       dict.Add("7", "Seven");       dict.Add("8", "Eight");       Console.WriteLine("Elements...");       IDictionaryEnumerator demoEnum = dict.GetEnumerator();       while (demoEnum.MoveNext()) {     ... Read More

Check whether an element is contained in the ArrayList in C#

AmitDiwan
Updated on 11-Dec-2019 06:38:41

125 Views

To check whether an element is contained in the ArrayList, the code is as follows −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main(){       ArrayList list = new ArrayList();       list.Add("One");       list.Add("Two");       list.Add("Three");       list.Add("Four");       list.Add("Five");       list.Add("Six");       list.Add("Seven");       list.Add("Eight");       Console.WriteLine("ArrayList elements...");       foreach(string str in list){          Console.WriteLine(str);       }       Console.WriteLine("ArrayList is read-only? = "+list.IsReadOnly);     ... Read More

Check whether a Hashtable contains a specific key or not in C#

AmitDiwan
Updated on 11-Dec-2019 06:35:31

86 Views

To check whether a Hashtable contains a specific key or not, the code is as follows −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main(){       Hashtable hash = new Hashtable();       hash.Add("One", "Katie");       hash.Add("Two", "John");       hash.Add("Three", "Barry");       hash.Add("Four", "");       hash.Add("Five", "Harry");       hash.Add("Six", "F");       hash.Add("Seven", "Tom");       hash.Add("Eight", "Andy");       hash.Add("Nine", "I");       hash.Add("Ten", "Tim");       Console.WriteLine("Hashtable Key and Value pairs...");       foreach(DictionaryEntry ... Read More

Check the HybridDictionary for a specific key in C#

AmitDiwan
Updated on 11-Dec-2019 06:32:29

65 Views

To check the HybridDictionary for a specified key, the code is as follows −Example Live Demousing System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main(){       HybridDictionary dict = new HybridDictionary(5);       dict.Add("A", "AB");       dict.Add("B", "BC");       dict.Add("C", "DE");       dict.Add("D", "FG");       dict.Add("E", "HI");       Console.WriteLine("Key/Value pairs...");       foreach(DictionaryEntry d in dict)       Console.WriteLine("Key = "+d.Key + ", Value = " + d.Value);       Console.WriteLine("Does HybridDictionary contains the key C? = "+dict.Contains("C"));    } ... Read More

Convert Class in C#

AmitDiwan
Updated on 11-Dec-2019 06:29:45

518 Views

The Convert Class has methods to convert a base data type to another base data type. Let us see some examples −The Convert.ToBoolean() method in C# is used to convert a specified value to an equivalent Boolean value.SyntaxFollowing is the syntax −public static bool ToBoolean (string val, IFormatProvider provider);Above, Val is a string that contains the value of either TrueString or FalseString, whereas the provider is an object that supplies culture-specific formatting information.ExampleLet us now see an example to implement the Convert.ToBoolean() method − Live Demousing System; using System.Globalization; public class Demo {    public static void Main(){       ... Read More

Advertisements