Found 2587 Articles for Csharp

How to get Synchronize access to the Stack in C#?

AmitDiwan
Updated on 16-Dec-2019 07:30:01

120 Views

To get synchronize access to the Stack, the code is as follows −Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       Stack stack = new Stack();       stack.Push(100);       stack.Push(200);       stack.Push(300);       stack.Push(400);       stack.Push(500);       Console.WriteLine("Stack...");       foreach(Object ob in stack) {          Console.WriteLine(ob);       }       Console.WriteLine("Count of elements = "+stack.Count);             Console.WriteLine("Synchronize access...");       lock(stack.SyncRoot) {       ... Read More

Get the specified members of the current Type in C#?

AmitDiwan
Updated on 16-Dec-2019 07:15:22

110 Views

To get the specified members of the current Type, the code is as follows −Example Live Demousing System; using System.Reflection; public class Demo {    public static void Main() {       Type type = typeof(Subject);       try {          FieldInfo fieldInfo = type.GetField("SubName");          MemberInfo[] info = type.GetMember("SubName");          Console.Write("Members = ");          for (int i = 0; i < info.Length; i++)          Console.WriteLine(" {0}", info[i]);          Console.WriteLine("FieldInfo = {0}", fieldInfo);       }       catch ... Read More

Create a Queue from another collection in C#?

AmitDiwan
Updated on 16-Dec-2019 07:12:14

93 Views

To create a Queue from another collection, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       Queue queue = new Queue();       queue.Enqueue("One");       queue.Enqueue("Two");       queue.Enqueue("Three");       Console.WriteLine("Queue elements...");       foreach(string str in queue) {          Console.WriteLine(str);       }       Console.WriteLine("Array elements...");       Queue arr = new Queue(queue.ToArray());       foreach(string str in arr) {          Console.WriteLine(str);       }   ... Read More

Count the total number of elements in the List in C#?

AmitDiwan
Updated on 16-Dec-2019 07:03:45

605 Views

To count the total number of elements in the List, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(String[] args) {       List list = new List();       list.Add("One");       list.Add("Two");       list.Add("Three");       list.Add("Four");       list.Add("Five");       Console.WriteLine("Elements in List1...");       foreach (string res in list) {          Console.WriteLine(res);       }       Console.WriteLine("Count of elements in list = "+list.Count);       list.Clear();       ... Read More

StringDictionary Class in C#?

AmitDiwan
Updated on 16-Dec-2019 06:50:36

213 Views

The StringDictionay class implements a hash table with the key and the value strongly typed to be strings rather than objects.Following are the properties of the StringDictionary class −Sr.NoProperty & Description1CountGets the number of key/value pairs in the StringDictionary.2IsSynchronizedGets a value indicating whether access to the StringDictionary is synchronized (thread safe).3tem[String]Gets or sets the value associated with the specified key.4KeysGets a collection of keys in the StringDictionary..5SyncRootGets an object that can be used to synchronize access to the StringDictionary.6ValuesGets a collection of values in the StringDictionary.Following are some of the methods of the StringDictionary class −Sr.NoMethods & Description1Add(String, String)Adds an ... Read More

How to get Synchronize access to the HybridDictionary in C#?

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

112 Views

To get synchronize access to HybridDictionary, the code is as follows −Exampleusing System; using System.Collections; using System.Collections.Specialized; public class Demo {    public static void Main() {       HybridDictionary dict1 = new HybridDictionary();       dict1.Add("A", "Books");       dict1.Add("B", "Electronics");       dict1.Add("C", "Smart Wearables");       dict1.Add("D", "Pet Supplies");       dict1.Add("E", "Clothing");       dict1.Add("F", "Footwear");       Console.WriteLine("HybridDictionary1 elements...");       foreach(DictionaryEntry d in dict1) {          Console.WriteLine(d.Key + " " + d.Value);       }       Console.WriteLine("Is the HybridDictionary1 ... Read More

HybridDictionary Class in C#?

AmitDiwan
Updated on 16-Dec-2019 06:41:19

180 Views

The HybridDictionary class implements IDictionary by using a ListDictionary while the collection is small, and then switching to a Hashtable when the collection gets large.Following are the properties of the HybridDictionary class −Sr.NoProperty & Description1CountGets the number of key/value pairs contained in the HybridDictionary.2IsFixedSizeGets a value indicating whether the HybridDictionary has a fixed size.3IsReadOnlyGets a value indicating whether the HybridDictionary is read-only.4IsSynchronizedGets a value indicating whether the HybridDictionary is synchronized (thread safe).5Item[Object]Gets or sets the value associated with the specified key.6KeysGets an ICollection containing the keys in the HybridDictionary.7SyncRootGets an object that can be used to synchronize access to the ... Read More

How to get Synchronize access to an Array in C#?

AmitDiwan
Updated on 16-Dec-2019 06:35:10

150 Views

To get synchronize access to an array, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       Array intArr = new int[] {5, 10, 15, 20, 25, 30, 35, 40 };       Console.WriteLine("Integer array...");       foreach (int i in intArr)       Console.WriteLine(i);       Console.WriteLine("After applying lock on array...");       lock(intArr.SyncRoot) {          foreach (Object ob in intArr)          Console.WriteLine(ob);       }    } }OutputThis will produce the following output −Integer array... 5 ... Read More

Add an object to the end of Collection in C#

AmitDiwan
Updated on 16-Dec-2019 06:29:59

239 Views

To add an object to the end of Collection, the code is as follows −Exampleusing System; using System.Collections.ObjectModel; public class Demo {    public static void Main() {       Collection col = new Collection();       col.Add(10);       col.Add(20);       col.Add(30);       col.Add(40);       col.Add(50);       col.Add(60);       col.Add(70);       col.Add(80);       Console.WriteLine("Elements in the Collection...");       foreach(int val in col) {          Console.WriteLine(val);       }       Console.WriteLine("Does the collection has the ... Read More

Add a string to the end of the StringCollection in C#

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

109 Views

To add a string to the end of the StringCollection, the code is as follows −Exampleusing System; using System.Collections.Specialized; public class Demo {    public static void Main() {       StringCollection strCol = new StringCollection();       strCol.Add("John");       strCol.Add("Tim");       strCol.Add("Gary");       strCol.Add("Katie");       strCol.Add("Andy");       strCol.Add("Amy");       strCol.Add("Scarlett");       strCol.Add("Jacob");       Console.WriteLine("Elements in StringCollection...");       foreach(Object ob in strCol) {          Console.WriteLine(ob);       }       Console.WriteLine("Count of elements = "+strCol.Count); ... Read More

Advertisements