Found 2628 Articles for Csharp

Add element to HashSet in C#

AmitDiwan
Updated on 10-Dec-2019 09:47:07

240 Views

To add the element to HashSet, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main(String[] args){       HashSet set1 = new HashSet();       set1.Add("A");       set1.Add("B");       set1.Add("C");       set1.Add("D");       set1.Add("E");       set1.Add("F");       set1.Add("G");       set1.Add("H");       Console.WriteLine("Elements in HashSet1...");       foreach (string res in set1){          Console.WriteLine(res);       }       HashSet set2 = new HashSet();       ... Read More

Get the fields of the current Type in C#

AmitDiwan
Updated on 10-Dec-2019 09:45:21

183 Views

To get the fields 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(System.String);       FieldInfo [] fields = type.GetFields(BindingFlags.Static | BindingFlags.NonPublic);       Console.WriteLine ("Following are the non-public fields=");       foreach (FieldInfo myField in fields) {          Console.WriteLine(myField.ToString());       }    } }OutputThis will produce the following output −Following are the non-public fields= Int32 TrimHead Int32 TrimTail Int32 TrimBoth Int32 charPtrAlignConst Int32 alignConstExampleLet us see another example − Live Demousing System; using System.Reflection; public class Demo {   ... Read More

Get the handle for the Type of a specified object C#

AmitDiwan
Updated on 10-Dec-2019 09:41:48

161 Views

To get the handle for the Type of a specified object, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       Type type1 = typeof(System.Type);       RuntimeTypeHandle typeHandle = Type.GetTypeHandle(type1);       Type type = Type.GetTypeFromHandle(typeHandle);       Console.WriteLine("Attributes = " + type.Attributes);       Console.WriteLine("Type Referenced = "+ type);    } }OutputThis will produce the following output −Attributes = AutoLayout, AnsiClass, Class, Serializable, BeforeFieldInit Type Referenced = System.RuntimeTypeExampleLet us see another example − Live Demousing System; public class Demo {    public static void ... Read More

Check whether the Dictionary has the specified key or not in C#

AmitDiwan
Updated on 10-Dec-2019 09:39:46

86 Views

To check whether the Dictionary has the specified key or not, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       Dictionary dict =       new Dictionary();       dict.Add("One", "John");       dict.Add("Two", "Tom");       dict.Add("Three", "Jacob");       dict.Add("Four", "Kevin");       dict.Add("Five", "Nathan");       Console.WriteLine("Count of elements = "+dict.Count);       Console.WriteLine("Key/value pairs...");       foreach(KeyValuePair res in dict) {          Console.WriteLine("Key = {0}, Value = {1}", res.Key, res.Value); ... Read More

Indicate whether the specified Unicode character is white space in C#

AmitDiwan
Updated on 10-Dec-2019 08:16:29

139 Views

To indicate whether the specified Unicode character is white space, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       bool res;       char val = ' ';       Console.WriteLine("Value = "+val);       res = Char.IsWhiteSpace(val);       Console.WriteLine("Is the value whitespace? = "+res);    } }OutputThis will produce the following output −Value = Is the value whitespace? = TrueExampleLet us see another example − Live Demousing System; public class Demo {    public static void Main() {       bool res; ... Read More

StringCollection Class in C#

AmitDiwan
Updated on 10-Dec-2019 08:13:49

557 Views

The StringCollection class represents a collection of strings. Following are the properties of the StringCollection class −Sr.noProperty & Description1CountGets the number of key/values pairs contained in the OrderedDictionary collection.2IsReadOnlyGets a value indicating whether the StringCollection is read-only..3IsSynchronizedGets a value indicating whether access to the StringCollection is synchronized (thread safe).4Item[Int32]Gets or sets the element at the specified index.5SyncRootGets an object that can be used to synchronize access to the StringCollection.Following are the methods of the StringCollection class −Sr.noMethod & Description1Add(String)Adds a string to the end of the StringCollection.2AddRange(String[])Copies the elements of a string array to the end of the StringCollection.3Clear()Removes all ... Read More

How to find the Capacity of a StringBuilder in C#

AmitDiwan
Updated on 10-Dec-2019 08:08:03

55 Views

To find the capacity of a StringBuilder in C#, the code is as follows −Example Live Demousing System; using System.Text; public class Demo {    public static void Main(String[] args) {       StringBuilder strBuilder1 = new StringBuilder("Tim");       StringBuilder strBuilder2 = new StringBuilder("Tom");       StringBuilder strBuilder3 = new StringBuilder();       Console.WriteLine("Capacity of StringBuilder1 = "+strBuilder1.Capacity);       Console.WriteLine("Capacity of StringBuilder2 = "+strBuilder2.Capacity);       Console.WriteLine("Capacity of StringBuilder3 = "+strBuilder3.Capacity);       strBuilder2 = strBuilder3;       Console.WriteLine("Is StringBuilder3 equal to StringBuilder2? = "+strBuilder3.Equals(strBuilder2));    } }OutputThis will produce the ... Read More

Total number of elements present in an array in C#

AmitDiwan
Updated on 10-Dec-2019 08:05:46

101 Views

To get the total number of elements present in an array, the code is as follows −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("Array length = "+products.GetLength(0));       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

How to create the StringBuilder in C#?

AmitDiwan
Updated on 10-Dec-2019 08:02:32

90 Views

To create StringBuilder in C#, the code is as follows −Example Live Demousing System; using System.Text; public class Demo {    public static void Main(String[] args) {       StringBuilder strBuilder1 = new StringBuilder("Tim");       StringBuilder strBuilder2 = new StringBuilder("Tom");       Console.WriteLine("Is StringBuilder1 equal to StringBuilder2? = "+strBuilder1.Equals(strBuilder2));    } }OutputThis will produce the following output −Is StringBuilder1 equal to StringBuilder2? = FalseExampleLet us see another example − Live Demousing System; using System.Text; public class Demo {    public static void Main(String[] args) {       StringBuilder strBuilder1 = new StringBuilder("John");       StringBuilder strBuilder2 = new StringBuilder("John");       ... Read More

How to create an OrderedDictionary in C#?

AmitDiwan
Updated on 10-Dec-2019 07:59:41

62 Views

To create an OrderedDictionary in C#, 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");       dict.Add("E", "Clothing");       dict.Add("F", "Footwear");       Console.WriteLine("OrderedDictionary elements...");       foreach(DictionaryEntry d in dict) {          Console.WriteLine(d.Key + " " + d.Value);       }       Console.WriteLine("Count of ... Read More

Advertisements