Found 2587 Articles for Csharp

ContainsKey() method in C#

karthikeya Boyini
Updated on 23-Jun-2020 08:11:40

287 Views

Set a Hashtable collection and add some elements to it.Hashtable h = new Hashtable(); h.Add(1, "Sam"); h.Add(2, "Jack"); h.Add(3, "Andy"); h.Add(4, "Katie"); h.Add(5, "Beth"); h.Add(6, "Benjamin");Use the ContainsKey() method to check whether a key exists in a Hashtable or not.Let’s check for key 3. It returns True of the key is found.h.ContainsKey(3));Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       Hashtable h = new Hashtable();       h.Add(1, "Sam");       h.Add(2, "Jack");       h.Add(3, "Andy");       h.Add(4, "Katie");       h.Add(5, "Beth");   ... Read More

Convert.ToDouble Method in C#

Samual Sam
Updated on 23-Jun-2020 08:12:13

10K+ Views

To convert a specified value to a double-precision floating-point number, use Convert.ToDouble() method.The following is our long value −long[] val = { 340, -200};Now convert it to Double.double result; result = Convert.ToDouble(val);Example Live Demousing System; public class Demo {    public static void Main() {       long[] val = { 340, -200};       double result;       // long to double       foreach (long number in val) {          result = Convert.ToDouble(number);          Console.WriteLine("Converted {0} value to {1}",number, result);       }    } }OutputConverted 340 value to 340 Converted -200 value to -200

C# Program to find a value in a Hashtable

George John
Updated on 23-Jun-2020 08:12:48

310 Views

Set Hahtable collection with elements.Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");Let’s say now you need to find a value, then use the ContainsValue() method.We are finding value “Chris” here −h.ContainsValue(“Chris”);Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       Hashtable h = new Hashtable();       h.Add(1, "Jack");       h.Add(2, "Henry");       h.Add(3, "Ben");       h.Add(4, "Chris");       Console.WriteLine("Keys and Values list:");       foreach (var key in h.Keys ) {          Console.WriteLine("Key ... Read More

C# Program to find a key in a Hashtable

karthikeya Boyini
Updated on 23-Jun-2020 08:13:17

462 Views

Set Hashtable collection with elements.Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");Let’s say now you need to find any key, then use the Contains() method. We are finding key 3 here −h.Contains(3);The following is the complete example.Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       Hashtable h = new Hashtable();       h.Add(1, "Jack");       h.Add(2, "Henry");       h.Add(3, "Ben");       h.Add(4, "Chris");       Console.WriteLine("Keys and Values list:");       foreach (var key in h.Keys ) ... Read More

Remove an item from a Hashtable in C#

Ankith Reddy
Updated on 23-Jun-2020 08:13:54

744 Views

The following is our Hashtable −Hashtable h = new Hashtable(); h.Add(1, "Jack"); h.Add(2, "Henry"); h.Add(3, "Ben"); h.Add(4, "Chris");To remove an item, use the Remove() method. Here, we are removing the 3rd element.h.Remove(3);Let us see the complete example.Example Live Demousing System; using System.Collections; public class Demo {    public static void Main() {       Hashtable h = new Hashtable();       h.Add(1, "Jack");       h.Add(2, "Henry");       h.Add(3, "Ben");       h.Add(4, "Chris");       Console.WriteLine("Initial list:");       foreach (var key in h.Keys ) {          Console.WriteLine("Key = ... Read More

C# Currency ("C") Format Specifier

Samual Sam
Updated on 23-Jun-2020 07:58:36

32K+ Views

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount.Let us see an example.double value = 139.87;Now to display the above number until three decimal places, use (“C3”) currency format specifier.value.ToString("C3", CultureInfo.CurrentCulture)Let us see another example.Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double value = 234.66;       // displays $       Console.WriteLine(value.ToString("C", CultureInfo.CurrentCulture));       Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));    } }Output$234.66 $234.660

LinkedList Contains Method in C#

Arjun Thakur
Updated on 23-Jun-2020 07:59:13

540 Views

Here is our LinkedList.int [] num = {1, 3, 7, 15}; LinkedList list = new LinkedList(num);To check whether the list contains an element or not, use the Contains() method. The following example checks for node 3 in the list.list.Contains(3)Above, returns True since the element is found as shown below −Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       int [] num = {1, 3, 7, 15};       LinkedList list = new LinkedList(num);       foreach (var n in list) {          Console.WriteLine(n);       }   ... Read More

How to negate the positive elements of an integer array in C#?

Chandu yadav
Updated on 23-Jun-2020 08:00:23

413 Views

The following is the array and its elements −int[] arr = { 10, 20, 15 };Set negative value to positive elements.if (arr[i] > 0) arr[i] = -arr[i];Loop the above until the length of the array.for (int i = 0; i < arr.Length; i++) {    Console.WriteLine(arr[i]);    if (arr[i] > 0)    arr[i] = -arr[i]; }Let us see the complete example.Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       int[] arr = { 10, 20, 15 };       Console.WriteLine("Displaying elements...");       for (int i = 0; i < ... Read More

Long Date ("D") Format Specifier in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:59:35

354 Views

The "D" format specifier represents a custom date and time format string.The format string is defined by culture's DateTimeFormatInfo.LongDatePattern property.The custom format string is −dddd, dd MMMM yyyyExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime myDate = new DateTime(2018, 9, 20);       Console.WriteLine(myDate.ToString("D", CultureInfo.CreateSpecificCulture("en-US")));    } }OutputThursday, September 20, 2018

The "%" custom specifier in C#

Samual Sam
Updated on 23-Jun-2020 08:06:03

166 Views

A % in a format string would multiply a number by 100 before it is formatted.The percent symbol is added in the number at the location where the % appears.For an example, declare and initialize a double variable.double d = .045;Now, use % custom specifier to multiply number by 100.d.ToString("#0.##%", CultureInfo.InvariantCulture)Example Live Demousing System; using System.Globalization; class Demo {    static void Main() {       double d = .045;       Console.WriteLine(d.ToString("#0.##%", CultureInfo.InvariantCulture));       Console.WriteLine(String.Format(CultureInfo.InvariantCulture, "{0:#0.##%}", d));    } }Output4.5% 4.5%

Advertisements