Samual Sam has Published 2492 Articles

C# program to find Largest, Smallest, Second Largest, Second Smallest in a List

Samual Sam

Samual Sam

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

2K+ Views

Set the listvar val = new int[] {    99,    35,    26,    87 };Now get the largest number.val.Max(z => z);Smallest numberval.Min(z => z);Second largest numberval.OrderByDescending(z => z).Skip(1).First();Second smallest numberval.OrderBy(z => z).Skip(1).First();The following is the code −Example Live Demousing System; using System.Linq; public class Program {    public ... Read More

What is abstraction in C#?

Samual Sam

Samual Sam

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

2K+ Views

Abstraction and encapsulation are related features in object-oriented programming. Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.Abstraction can be achieved using abstract classes in C#. C# allows you to create abstract classes that are used to provide a partial class ... Read More

C# program to print duplicates from a list of integers

Samual Sam

Samual Sam

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

668 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

75 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

242 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

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

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

338 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

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

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

Advertisements