Found 2587 Articles for Csharp

Count the number of key/value pairs in the Hashtable in C#

AmitDiwan
Updated on 11-Dec-2019 10:17:20

343 Views

To count the number of key/value pairs in the Hashtable, 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("A", "SUV");       hash.Add("B", "MUV");       hash.Add("C", "AUV");       Console.WriteLine("Hashtable elements...");       foreach(DictionaryEntry d in hash) {          Console.WriteLine(d.Key + " " + d.Value);       }       Console.WriteLine("Count of Key/value pairs = "+hash.Count);       hash.Add("D", "Utility Vehicle");       hash.Add("E", "Convertible"); ... Read More

Insert an object at the top of the Stack in C#

AmitDiwan
Updated on 11-Dec-2019 10:12:29

247 Views

To insert an object at the top of the Stack, the code is as follows −Example Live Demousing System; using System.Collections.Generic; public class Demo {    public static void Main() {       Stack stack = new Stack();       stack.Push(100);       stack.Push(150);       stack.Push(175);       stack.Push(200);       stack.Push(225);       stack.Push(250);       Console.WriteLine("Elements in the Stack:");       foreach(var val in stack) {          Console.WriteLine(val);       }       Console.WriteLine("Count of elements in the Stack = "+stack.Count);       ... Read More

Insert an element into the ArrayList at the specified index in C#

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

307 Views

To insert an element into the ArrayList at the specified index, 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? ... Read More

Get the TypeCode for value type UInt32 in C#

AmitDiwan
Updated on 11-Dec-2019 10:04:29

125 Views

To get the TypeCode for value type UInt32, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       uint val1 = 55;       uint val2 = 100;       TypeCode type1 = val1.GetTypeCode();       TypeCode type2 = val2.GetTypeCode();       Console.WriteLine("Typecode for val1 = "+type1);       Console.WriteLine("Typecode for val2 = "+type2);    } }OutputThis will produce the following output −Typecode for val1 = UInt32 Typecode for val2 = UInt32ExampleLet us see another example − Live Demousing System; public class Demo {   ... Read More

Get all the interfaces implemented or inherited by the current Type in C#

AmitDiwan
Updated on 11-Dec-2019 10:01:43

534 Views

To get all the interfaces implemented or inherited by the current Type, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       Type type = typeof(float);       Type myInterface = type.GetInterface("IFormattable", true);       Type[] myInterfaces = type.GetInterfaces();       Console.WriteLine("Interface = "+myInterface);       Console.WriteLine("All the Interfaces...");       for (int i = 0; i < myInterfaces.Length; i++)       Console.WriteLine(""+myInterfaces[i]);    } }OutputThis will produce the following output −Interface = System.IFormattable All the Interfaces... System.IComparable System.IFormattable System.IConvertible System.IComparable`1[System.Single] System.IEquatable`1[System.Single]Let us see another example −Example Live ... Read More

Get a specific interface implemented or inherited by the current Type in C#

AmitDiwan
Updated on 11-Dec-2019 09:59:31

122 Views

To get a specific interface implemented or inherited by the current Type, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       Type type = typeof(double);       Type myInterface = type.GetInterface("IFormattable");       Console.WriteLine("Interface = "+myInterface);    } }OutputThis will produce the following output −Interface = System.IFormattableExampleLet us see another example − Live Demousing System; public class Demo {    public static void Main() {       Type type = typeof(float);       Type myInterface = type.GetInterface("IFormattable",true);       Console.WriteLine("Interface = "+myInterface);    } }OutputThis will produce the following output −Interface = System.IFormattable

Get the TypeCode for value type UInt64 in C#

AmitDiwan
Updated on 11-Dec-2019 08:16:17

153 Views

To get the TypeCode for value type UInt64, the code is as follows −Example Live Demousing System; public class Demo {    public static void Main() {       ulong val1 = 55;       ulong val2 = 100;       TypeCode type1 = val1.GetTypeCode();       TypeCode type2 = val2.GetTypeCode();       Console.WriteLine("Typecode for val1 = "+type1);       Console.WriteLine("Typecode for val2 = "+type2);    } }OutputThis will produce the following output −Typecode for val1 = UInt64 Typecode for val2 = UInt64ExampleLet us see another example −using System; public class Demo {    public ... Read More

Check whether the Dictionary contains a specific value or not in C#

AmitDiwan
Updated on 11-Dec-2019 08:13:50

442 Views

To check whether the Dictionary contains a specific value 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

Method Parameters in C#

AmitDiwan
Updated on 11-Dec-2019 08:08:00

518 Views

The parameters are used to pass and receive data from a method. Let us first see the syntax −Access Specifier − This determines the visibility of a variable or a method from another class.Return type − A method may return a value. The return type is the data type of the value the method returns. If the method is not returning any values, then the return type is void.Method name − Method name is a unique identifier and it is case sensitive. It cannot be same as any other identifier declared in the class.Parameter list − Enclosed between parentheses, the ... Read More

Reverse the order of the elements in the entire List or in the specified range in C#

AmitDiwan
Updated on 11-Dec-2019 08:05:55

261 Views

To reverse the order of the elements in the entire 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");       list.Add("Six");       list.Add("Seven");       list.Add("Eight");       Console.WriteLine("Enumerator iterates through the list elements...");       List.Enumerator demoEnum = list.GetEnumerator();       while (demoEnum.MoveNext()) {          string res ... Read More

Advertisements