Karthikeya Boyini has Published 2193 Articles

How to iterate two Lists or Arrays with one foreach statement in C#?

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 15:18:04

6K+ Views

Set two arrays.var val = new [] { 20, 40, 60}; var str = new [] { "ele1", "ele2", "ele3"};Use the zip() method to process the two arrays in parallel.var res = val.Zip(str, (n, w) => new { Number = n, Word = w });The above fetches both the arrays ... Read More

C# Program to remove duplicate characters from String

karthikeya Boyini

karthikeya Boyini

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

11K+ Views

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

How to validate a string for a numeric representation using TryParse in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 15:14:57

358 Views

The following is our string −string myStr = "5";To check whether the above is a string with numeric representation, use TryParse and out.int.TryParse(myStr, out a);Here is the complete code.Example Live Demousing System.IO; using System; class Program {    static void Main() {       bool res;       int ... Read More

Program to get the last element from an array using C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 15:14:13

4K+ Views

Declare an array and add elements.int[] val = { 5, 8, 15, 25, 40, 55, 80, 100 };Now, use the Queryable Last() method to get the last element.val.AsQueryable().Last();Let us see the complete code.Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Demo {    static void Main() {       ... Read More

The Fibonacci sequence in Javascript

karthikeya Boyini

karthikeya Boyini

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

508 Views

Fibonacci numbers are the numbers such that every number in the series after the first two is the sum of the two preceding ones. The series starts with 1, 1. Example −1, 1, 2, 3, 5, 8, 13, 21, 34, ….We can write a program to generate nth as follows −functionfibNaive(n) {    if (n

C# Program to display temporary file names

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 14:55:40

234 Views

The GetTempPath() method in C# displays temporary file names −Path.GetTempPath();Get the names in a variable and display −string tempFile = Path.GetTempPath();The following is the code −Example Live Demousing System; using System.IO; class Demo {    static void Main() {       string tempFile = Path.GetTempPath();       Console.WriteLine(tempFile);    } }Output/tmp/

C# Program to find the largest element from an array

karthikeya Boyini

karthikeya Boyini

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

3K+ Views

Declare an array −int[] arr = { 20, 50, -35, 25, 60 };Now to get the largest element from an array, use the Max() method −arr.Max());Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       int[] arr = { 20, 50, -35, 25, 60 };       Console.WriteLine(arr.Max());    } }Output60

Group by Operator in C#

karthikeya Boyini

karthikeya Boyini

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

183 Views

Use the group by the operator in C# to separate the results of an expression into parts.Let’s say the following is our array −int[] a = { 5, 10, 15, 20, 25, 30 };Now, using Group by and orderby, we will find the elements greater than 20 −var check = ... Read More

Long.parse method in C#

karthikeya Boyini

karthikeya Boyini

Updated on 22-Jun-2020 14:50:11

2K+ Views

To convert a string to a long, use the Long.parse method in C# −Firstly, set a string −string str = "7864646475767";Now, convert it to long −long.Parse(str);Here is the complete code −Example Live Demousing System; using System.Linq; class Demo {    static void Main() {       string str = "7864646475767"; ... Read More

Round a number to the nearest even number in C#

karthikeya Boyini

karthikeya Boyini

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

781 Views

The ToEven property is used with the MidpointRounding Enumeration to round a number to the nearest even number.Declare and initialize a decimal number −decimal val = 25.55M;To round a number to the nearest even number −decimal.Round(val, 0, MidpointRounding.ToEven)Here is the complete code −Example Live Demousing System; using System.Linq; class Demo { ... Read More

Advertisements