Found 34489 Articles for Programming

C# Currency ("C") Format Specifier

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

27K+ 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

374 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

295 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

255 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

126 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%

Clear a Linked List in C#

George John
Updated on 23-Jun-2020 08:05:42

136 Views

Clear a LinkedList using Clear() method.Let us first set a LinkedList.string [] employees = {"Patrick", "Robert", "John", "Jacob", "Jamie"}; LinkedList list = new LinkedList(employees);Now, let us clear the LinkedList.list.Clear();Let us see the complete code.Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string [] employees = {"Patrick", "Robert", "John", "Jacob", "Jamie"};       LinkedListlist = new LinkedList(employees);       foreach (var emp in list) {          Console.WriteLine(emp);       }       // clearing list       list.Clear();       Console.WriteLine("LinkedList after removing the ... Read More

C# Enum Parse Method

Ankith Reddy
Updated on 23-Jun-2020 08:07:12

1K+ Views

The Parse method in Enum converts the string representation of the name or numeric value of enum constants to an equivalent enumerated object.The following is our enumeration.enum Vehicle { Car, Bus, Truck, Motobike };Now, use the GetNames() method in a loop to get the enum values. Parse them using the Enum.Parse() method as shown below −Enum.Parse(typeof(Vehicle)Example Live Demousing System; public class Demo {    enum Vehicle { Car, Bus, Truck, Motobike };    public static void Main() {       Console.WriteLine("The enumeration...");       foreach (string v in Enum.GetNames(typeof(Vehicle))) {          Console.WriteLine("{0} = {1:D}", v, Enum.Parse(typeof(Vehicle), ... Read More

C# Enum ToString() Method

karthikeya Boyini
Updated on 23-Jun-2020 08:06:27

2K+ Views

The ToString() method converts the value of this instance to its equivalent string representation.Firstly, set an enum.enum Vehicle { Car, Bus, Truck, Motobike };To convert it to an equivalent string representation, use ToString().Vehicle.Car.ToString("d")Example Live Demousing System; public class Demo {    enum Vehicle { Car, Bus, Truck, Motobike };    public static void Main() {       Console.WriteLine("Vehicle.Car = {0}", Vehicle.Car.ToString("d"));       Console.WriteLine("Vehicle.Bus = {0}", Vehicle.Bus.ToString("d"));    } }OutputVehicle.Car = 0 Vehicle.Bus = 1

Short Date ("d") Format Specifier

Samual Sam
Updated on 23-Jun-2020 08:07:54

212 Views

The "d" format specifier represents a custom date and time format string.The format string is defined by culture's DateTimeFormatInfo.ShortDatePattern property.The custom format string is −MM/dd/yyyyExample Live Demousing System; using System.Globalization; class Demo {    static void Main() {       DateTime myDate = new DateTime(2018,9, 09);       Console.WriteLine(myDate.ToString("d", DateTimeFormatInfo.InvariantInfo));       Console.WriteLine(myDate.ToString("d", CultureInfo.CreateSpecificCulture("en-US")));    } }Output09/09/2018 9/9/2018

LinkedList Clear() Method in C#

karthikeya Boyini
Updated on 23-Jun-2020 07:50:00

564 Views

Use the Clear() method to clear a LinkedList. This will remove all the nodes from the LinkedList.Let’s say the following is our LinkedList −int [] num = {30, 65, 80, 95, 110, 135}; LinkedList list = new LinkedList(num);To clear the LinkedList.list.Clear();Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       int [] num = {30, 65, 80, 95, 110, 135};       LinkedList list = new LinkedList(num);       foreach (var n in list) {          Console.WriteLine(n);       }       // clear       list.Clear();       Console.WriteLine("No node in the LinkedList now...");       foreach (var n in list) {          Console.WriteLine(n);       }    } }Output30 65 80 95 110 135 No node in the LinkedList now...

Advertisements