Samual Sam has Published 2310 Articles

C# program to print duplicates from a list of integers

Samual Sam

Samual Sam

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

923 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 ... Read More

How to find date difference in C#?

Samual Sam

Samual Sam

Updated on 22-Jun-2020 09:18:41

113 Views

To find the difference between two dates in C#, you need to first set two dates to be compared using the DateTime object. We will use the DateTime class in C#.Date 1DateTime date1 = new DateTime(2018, 09, 15); Console.WriteLine("Date 1 : {0}", date1);Date 2DateTime date2 = new DateTime(2018, 09, 28); ... Read More

How to print a diamond using nested loop using C#?

Samual Sam

Samual Sam

Updated on 22-Jun-2020 09:15:34

374 Views

With C#, you can easily display the following diamond shape.$ $$$ $$$$$ $$$$$$$ $$$$$$$$$ $$$$$$$ $$$$$ $$$ $To display a diamond shape, you need to focus on the following points −Number of rows Dollar sign to be displayed Empty spacesConsidering the above you can easily create the diamond shape as ... Read More

How to check if a string contains a certain word in C#?

Samual Sam

Samual Sam

Updated on 22-Jun-2020 09:12:21

17K+ Views

Use the Contains() method to check if a string contains a word or not.Set the string −string s = "Together we can do so much!";Now let’s say you need to find the word “much”if (s.Contains("much") == true) {    Console.WriteLine("Word found!"); }Let us see the complete code −Example Live Demousing System; ... Read More

How to remove an element from Array List in C#?

Samual Sam

Samual Sam

Updated on 22-Jun-2020 09:07:43

2K+ Views

Declare a new ArrayList and add elements to it.ArrayList arr = new ArrayList(); arr.Add( "One" ); arr.Add( "Two" ); arr.Add( "Three" ); arr.Add( "Four" );Now let’s say you need to remove the element “Three”. For that, use the Remove() method.arr.Remove("Three");The following is the complete example to remove an element from ... Read More

How to find a number using Pythagoras Theorem using C#?

Samual Sam

Samual Sam

Updated on 22-Jun-2020 09:04:37

543 Views

Firstly, set the first two numbers −double val1, val2; val1 = 10; val2 = 5;Now use the Math.Sqrt function to use Pythagoras Theorem.res = Math.Sqrt(val1 * val1 + val2 * val2);Example Live Demousing System; public class Program {    public static void Main(string[] args) {       double val1, val2, ... Read More

How to read a line from the console in C#?

Samual Sam

Samual Sam

Updated on 22-Jun-2020 08:56:44

2K+ Views

The ReadLine() method is used to read a line from the console in C#.str = Console.ReadLine();The above will set the line in the variable str.Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string str;       // use ReadLine() ... Read More

How to remove an item from a C# list by using an index?

Samual Sam

Samual Sam

Updated on 22-Jun-2020 08:54:42

13K+ Views

To remove an item from a list in C# using index, use the RemoveAt() method.Firstly, set the list −List list1 = new List() {    "Hanks",    "Lawrence",    "Beckham",    "Cooper", };Now remove the element at 2nd position i.e. index 1list1.RemoveAt(1);Let us see the complete example −Example Live Demousing System; ... Read More

Queue Interface In C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 08:45:39

453 Views

Queue represents a first-in, first out collection of object. It is used when you need a first-in, first-out access to items. When you add an item to the list, it is called enqueue, and when you remove an item, it is called deque.Let us see an example of the Queue ... Read More

Chained Exceptions in C#

Samual Sam

Samual Sam

Updated on 22-Jun-2020 08:27:06

683 Views

Chained Exceptions are a chain of try-catch statements that handle exceptions. To create a chain of exceptions i.e. chained exceptions −Set the first try-catch −Examplestatic void Main(string[] args) {    try {       One();    } catch (Exception e) {       Console.WriteLine(e);    } }Now try-catch ... Read More

Advertisements