Ankith Reddy has Published 996 Articles

C# Program to display the first element from an array

Ankith Reddy

Ankith Reddy

Updated on 23-Jun-2020 06:51:22

2K+ Views

The following is our array −double[] myArr = {20.5, 35.6, 45.7, 55.6, 79.7};To get the first element, use the First() method.myArr.AsQueryable().First();Let us see the complete code −Example Live Demousing System; using System.Linq; using System.Collections.Generic; class Demo {    static void Main() {       double[] myArr = {20.5, 35.6, 45.7, ... Read More

Enumerable.Repeat method in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 15:45:10

3K+ Views

Enumerable.Repeat method is part of System.Linq namespace.It returns a collection with repeated elements in C#.Firstly, set which element you want to repeat and how many times.As an example, let us see how to repeat number 10, five times −Enumerable.Repeat(10, 5);The following is the complete example −Example Live Demousing System; using System.Linq; ... Read More

Remove all duplicates from a given string in C#

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 15:43:10

465 Views

Here is the string.string str = "ppqqrr";Now, use Hashset to map the string to char. This will remove the duplicate characters from a string.var res = new HashSet(str);Let us see the complete example −Example Live Demousing System; using System.Linq; using System.Collections.Generic; namespace Demo {    class Program {       ... Read More

C# program to create a ValueType with names

Ankith Reddy

Ankith Reddy

Updated on 22-Jun-2020 15:35:11

151 Views

With C# 7, you can easily create a ValueType with names.Note − Add System.ValueTuple package to run ValueTuple program.Let’s see how to add it −Go to your projectRight click on the project in the solution explorerSelect “Manage NuGet Packages”You will reach the NuGet Package Manager.Now, click the Browse tab and ... Read More

Find missing number in a sequence in C#

Ankith Reddy

Ankith Reddy

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

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

204 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

910 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

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

506 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

552 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

Advertisements