Found 2628 Articles for Csharp

How to find the average of elements of an integer array in C#?

Samual Sam
Updated on 22-Jun-2020 09:56:58

147 Views

The following is our integer array −int[] myArr = new int[6] {    8,    4,    2,    5,    9,    14 };Firstly, get the length of the array, and loop through the array to find the sum of the elements. After that, divide it with the length.int len = myArr.Length; int sum = 0; int average = 0; for (int i = 0; i < len; i++) {    sum += myArr[i]; } average = sum / len;Here is the complete codeExample Live Demousing System; public class Program {    public static void Main() {       ... Read More

C# Program to replace a character with asterisks in a sentence

Ankith Reddy
Updated on 22-Jun-2020 09:57:18

939 Views

Use the Replace() method to replace a character with asterisks.Let’s say our string is −string str = "dem* text";To replace it, use the Replace() method −str.Replace('*', 'o');Here is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       string str = "dem* text";       Console.WriteLine("Initial string = " + str);       string res = str.Replace('*', 'o');       // after replacing       Console.WriteLine("After replacing asterisk = " + res.ToString());    } }OutputInitial string = dem* text After replacing asterisk = demo text

C# Program to replace a special character from a String

karthikeya Boyini
Updated on 22-Jun-2020 09:57:38

5K+ Views

Let’s say our string is −string str = "abcd$ef$gh";To replace the special character, use the Replace() method.string res = str.Replace('$', 'k');The following is the complete code to replace character from a string −Example Live Demousing System; public class Program {    public static void Main() {       string str = "abcd$ef$gh";       Console.WriteLine("Initial string = " + str);       string res = str.Replace('$', 'k');       // after replacing       Console.WriteLine("Replaced string = " + res.ToString());    } }OutputInitial string = abcd$ef$gh Replaced string = abcdkefkgh

C# program to remove all duplicates words from a given sentence

George John
Updated on 22-Jun-2020 09:44:04

1K+ Views

Set a string with duplicate words.string str = "One Two Three One";Above, you can see the word “One” comes twice.To remove dulicate words, you can try to run the following code in C# −Example Live Demousing System; using System.Linq; public class Program {    public static void Main() {       string str = "One Two Three One";       string[] arr = str.Split(' ');       Console.WriteLine(str);       var a =       from k in arr       orderby k       select k;       Console.WriteLine("After removing duplicate words..."); ... Read More

C# program to remove an item from Set

Chandu yadav
Updated on 22-Jun-2020 09:45:15

168 Views

Firstly, declare a HashSet and add elements −var names = new HashSet(); names.Add("Tim"); names.Add("John"); names.Add("Tom"); names.Add("Kevin");To remove an element, use RemoveWhere.names.RemoveWhere(x => x == "John");Let us see the complete example −Exampleusing System; using System.Collections.Generic; public class Program {    public static void Main() {       var names = new HashSet < string > ();       names.Add("Tim");       names.Add("John");       names.Add("Tom");       names.Add("Kevin");       Console.WriteLine("Initial Set...");       foreach(var val in names) {          Console.WriteLine(val);       }       names.RemoveWhere(x => x ... Read More

C# program to print unique values from a list

Samual Sam
Updated on 22-Jun-2020 09:44:45

5K+ Views

Set the list.List < int > list = new List < int > (); list.Add(99); list.Add(49); list.Add(32);To get unique elements.List myList = list.Distinct().ToList();Here is the complete example to display unique values from a list.Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < int > list = new List < int > ();       list.Add(55);       list.Add(45);       list.Add(55);       list.Add(65);       list.Add(73);       list.Add(24);       list.Add(65);       Console.WriteLine("Initial List..."); ... Read More

C# program to check if all the values in a list that are greater than a given value

Arjun Thakur
Updated on 22-Jun-2020 09:47:12

1K+ Views

Let’s say you need to find the elements in the following list to be greater than 80.int[] arr = new int[] {55, 100, 87, 45};For that, loop until the array length. Here, res = 80 i.e. the given element.for (int i = 0; i < arr.Length; i++) {    if(arr[i] res) {                Console.WriteLine(arr[i]);             }          }       }    } }

C# program to remove duplicate elements from a List

karthikeya Boyini
Updated on 22-Jun-2020 09:46:18

471 Views

Declare a list and add elements.List list = new List(); list.Add(50); list.Add(90); list.Add(50); list.Add(100);Now, use Distinct() method to get the unique elements only.List myList = list.Distinct().ToList();The following is the complete code to remove duplicate elements from a List −Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Demo {    public static void Main() {       List < int > list = new List < int > ();       list.Add(50);       list.Add(90);       list.Add(50);       list.Add(100);       Console.WriteLine("Initial List...");       foreach(int a in list) ... Read More

C# program to check if two lists have at-least one element common

Samual Sam
Updated on 22-Jun-2020 09:49:07

669 Views

Set the first list.int[] arr1 = {    65,    57,    63,    98 };Now, set the second list.int[] arr2 = {    43,    65,    33,    57 };Let us now see the complete code to check if two lists have common elements using == and < operators.Example Live Demousing System; using System.Collections.Generic; using System.Linq; public class Program {    public static void Main() {       int[] arr1 = {          65,          57,          63,          98       }; ... Read More

How to find the length of jagged array using a property?

Ankith Reddy
Updated on 22-Jun-2020 09:49:50

961 Views

Firstly, declare and initialize a jagged array.int[][] arr = new int[][] { new int[] {    0,    0 }, new int[] {    1,    2 }, new int[] {    2,    4 }, new int[] {    3,    6 }, new int[] {    4,    8 } };Now use the length property to get the length.arr.LengthThe following is the complete code −Example Live Demousing System; public class Program {    public static void Main() {       int[][] arr = new int[][]{new int[]{0,0},new int[]{1,2}, new int[]{2,4},new int[]{ 3, 6 }, new       int[]{ 4, 8 } };       // Length       Console.WriteLine("Length:"+arr.Length);       Console.WriteLine("Upper Bound: {0}",arr.GetUpperBound(0).ToString());       Console.WriteLine("Lower Bound: {0}",arr.GetLowerBound(0).ToString());       Console.WriteLine("Dimensions of Array : " + arr.Rank);    } }OutputLength:5 Upper Bound: 4 Lower Bound: 0 Dimensions of Array : 1

Advertisements