Found 2587 Articles for Csharp

C# Program to perform Currency Conversion

Chandu yadav
Updated on 22-Jun-2020 09:19:32

4K+ Views

Let’s say you need to get the value of 10 dollars in INR.Firstly, set the variables: double usd, inr, val;Now set the dollars and convert it to INR.// how many dpllars usd = 10; // current value of US$ val = 69; inr = usd * val;Let us see the complete code −Example Live Demousing System; namespace Demo {    public class Program {       public static void Main(string[] args) {          Double usd, inr, val;          // how many dpllars          usd = 10;          // current value of US$          val = 69;          inr = usd * val;          Console.WriteLine("{0} Dollar = {1} INR", usd, inr);       }    } }Output10 Dollar = 690 INR

C# program to print all distinct elements of a given integer array in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:22:53

574 Views

We have set an array and a dictionary to get the distinct elements.int[] arr = {    88,    23,    56,    96,    43 }; var d = new Dictionary < int, int > ();Dictionary collection allows us to get the key and value of a list.The following is the code to display distinct elements of a given integer array −Example Live Demousing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {             88,   ... Read More

C# program to determine if any two integers in array sum to given integer

Arjun Thakur
Updated on 22-Jun-2020 09:21:51

1K+ Views

The following is our array −int[] arr = new int[] {    7,    4,    6,    2 };Let’s say the given intger that should be equal to sum of two other integers is −int res = 8;To get the sum and find the equality.for (int i = 0; i < arr.Length; i++) {    for (int j = 0; j < arr.Length; j++) {       if (i != j) {          int sum = arr[i] + arr[j];          if (sum == res) {             Console.WriteLine(arr[i]); ... Read More

How to convert a list to string in C#?

Ankith Reddy
Updated on 22-Jun-2020 09:25:18

3K+ Views

Declare a list.List < string > l = new List < string > ();Now, add elements to the list.// elements l.Add("Accessories"); l.Add("Footwear"); l.Add("Watches");Now convert it into a string.string str = string.Join(" ", l.ToArray());Let us see the final code to convert a list to string in C# −Exampleusing System; using System.Collections.Generic; class Demo {    static void Main() {       List < string > l = new List < string > ();       // elements       l.Add("Accessories");       l.Add("Footwear");       l.Add("Watches");       string str = string.Join(" ", l.ToArray());       Console.WriteLine(str);    } }

C# program to print duplicates from a list of integers

Samual Sam
Updated on 22-Jun-2020 09:24:09

922 Views

To print duplicates from a list of integers, use the ContainsKey.Below, we have first set the integers.int[] arr = {    3,    6,    3,    8,    9,    2,    2 };Then Dictionary collection is used to get the count of duplicate integers.Let us see the code to get duplicate integers.Example Live Demousing System; using System.Collections.Generic; namespace Demo {    public class Program {       public static void Main(string[] args) {          int[] arr = {             3,             6,     ... Read More

Formatted string literals in C#

karthikeya Boyini
Updated on 22-Jun-2020 09:26:44

576 Views

To format string literals in C#, use the String.Format method.In the following example, 0 is the index of the object whose string value gets inserted at that particular position −using System; namespace Demo {    class Test {       static void Main(string[] args) {          decimal A = 15.2 m;          string res = String.Format("Temperature = {0}°C.", A);          Console.WriteLine(res);       }    } }In the following example, let us format string for double type.Example Live Demousing System; class Demo {    public static void Main(String[] args) { ... Read More

Formatted output in C#

George John
Updated on 22-Jun-2020 09:26:01

2K+ Views

To format output in C#, let us see examples to format date and double type.Set formatted output for Double type.Example Live Demousing System; class Demo {    public static void Main(String[] args) {       Console.WriteLine("Three decimal places...");       Console.WriteLine(String.Format("{0:0.000}", 987.383));       Console.WriteLine(String.Format("{0:0.000}", 987.38));       Console.WriteLine(String.Format("{0:0.000}", 987.7899));       Console.WriteLine("Thousands Separator...");       Console.WriteLine(String.Format("{0:0, 0.0}", 54567.46));       Console.WriteLine(String.Format("{0:0, 0}", 54567.46));    } }OutputThree decimal places... 987.383 987.380 987.790 Thousands Separator... 54, 567.5 54, 567Set formatted output for DateTimeExample Live Demousing System; static class Demo {    static void Main() ... Read More

How to find a matching substring using regular expression in C#?

Samual Sam
Updated on 30-Jul-2019 22:30:23

2K+ Views

Our string is − string str = " My make "; Use the following regular expression to find the substring “make” @"\bmake\b" Here is the complete code − Example Live Demo using System; using System.Text.RegularExpressions; namespace RegExApplication { public class Program { private static void showMatch(string text, string expr) { Console.WriteLine("The Expression: " + expr); MatchCollection mc = Regex.Matches(text, expr); ... Read More

How do you make code reusable in C#?

Chandu yadav
Updated on 22-Jun-2020 09:27:05

2K+ Views

To make code reusable in C#, use Interfaces. Interfaces define properties, methods, and events, which are the members of the interface. Interfaces contain only the declaration of the members. It is the responsibility of the deriving class to define the members. It often helps in providing a standard structure that the deriving classes would follow.For example, Shape Interface −public interface IShape {    void display(); }Above we have declared an Interface Shape. You can notice that it begins with a capital “I”. It is a common convention that interfaces names begin with “I”.We have not added an access specifier above ... Read More

How to check if an item exists in a C# list collection?

Arjun Thakur
Updated on 22-Jun-2020 09:09:58

14K+ Views

Set a list −List < string > list1 = new List < string > () {    "Lawrence",    "Adams",    "Pitt",    "Tom" };Now use the Contains method to check if an item exits in a list or not.if (list1.Contains("Adams") == true) {    Console.WriteLine("Item exists!"); }The following is the code to check if an item exists in a C# list or not.Exampleusing System; using System.Collections.Generic; public class Program {    public static void Main() {       List < string > list1 = new List < string > () {          "Lawrence",   ... Read More

Advertisements