Ankith Reddy has Published 1070 Articles

Find missing number in a sequence in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 15:33:16

796 Views

Set a list.List myList = new List(){1, 2, 3, 5, 8, 9};Now, get the first and last elements −int a = myList.OrderBy(x => x).First(); int b = myList.OrderBy(x => x).Last();In a new list, get all the elements and use the Except to get the missing numbers −List myList2 = Enumerable.Range(a, ... Read More

C# program to get the total number of cores on a computer

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 15:03:52

147 Views

Use the Environment.ProcessorCount to get the total number of cores on a computer −Environment.ProcessorCountThe following is the code that displays the total number of cores on a computer in C# −ExampleUsing System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine(Environment.ProcessorCount);       }    } }

Func generic type in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 15:00:01

676 Views

The Func generic type store anonymous methods and is a parameterized type.In the below example, we have 4 func type instance −The first type receives int and returns stringFunc one = (p) => string.Format("{0}", p);The second type receives bool & long and returns stringFunc two = (q, p) =>string.Format("{0} and ... Read More

C# Program to generate random lowercase letter

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 14:54:23

1K+ Views

Firstly, set Random class −Random random = new Random();Set a range under the Next() method. This displays a letter between 0 and 26.int a = random.Next(0, 26);Here is the complete code −Example Live Demousing System; using System.IO; using System.Linq; class Demo {    static void Main() {       Random ... Read More

SequenceEqual method in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 14:47:37

255 Views

The SequenceEqual method is used to test collections for equality.Let us set three string arrays −string[] arr1 = { "This", "is", "it" }; string[] arr2 = { "My", "work", "report" }; string[] arr3 = { "This", "is", "it" };Now, compare the first array with the second using the SequenceEqual() method ... Read More

C# Program to write a number in hexadecimal format

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 14:45:32

400 Views

Let’s say the following is the number −int a = 12250;You can work around the following ways to get a number in hexadecimal format −{0:x} {0:x8} {0:X} {0:X8}Here is the code −Example Live Demousing System; class Demo {    static void Main() {       int a = 12250;   ... Read More

C# Program to get the name of the drive

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 14:33:38

146 Views

Set the drive for which you want to display the name −DriveInfo info = new DriveInfo("C");Now, to get the drive name, use the Name property −info.NameHere is the complete code with output −Exampleusing System; using System.Linq; using System.IO; public class Demo {    public static void Main() {     ... Read More

TakeWhile method in C# ()

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 14:31:14

376 Views

With the TakeWhile() method, you can get methods by setting a condition base on Predicate.Firstly, declare and initialize an array −int[] arr = { 25, 40, 65, 70};Now, use the TakeWhile() method and predicate to get all the elements that are less than 30.var val = arr.TakeWhile(ele => ele < ... Read More

C# Program to set the timer to zero

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 14:18:51

366 Views

To set the timer information to zero, use the Stopwatch Restart method.Firstly, begin the Stopwatch −Stopwatch s = Stopwatch.StartNew();Then, use Restart() to set the timer to zero −s.Restart();Let us see the complete code −ExampleLive Demousing System; using System.Threading; using System.Diagnostics; public class Demo {    public static void Main() { ... Read More

ContainsKey in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 14:17:07

1K+ Views

ContainsKey is a Dictionary method in C# and check whether a key exists in the Dictionary or not.Declare a Dictionary and add elements −var dict = new Dictionary() {    {"TV", 1},    {"Home Theatre", 2},    {"Amazon Alexa", 3},    {"Google Home", 5},    {"Laptop", 5},    {"Bluetooth Speaker", ... Read More

Advertisements