Found 34494 Articles for Programming

How to remove an empty string from a list of empty strings in C#?

karthikeya Boyini
Updated on 22-Jun-2020 09:06:28

881 Views

Firstly, set a list with empty string as elements.List myList = new List() {    " ",    " ",    " " };Now let us remove one empty element using index.myList.RemoveAt(0);Example Live Demousing System; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       List myList = new List() {          " ",          " ",          " "       };       Console.Write("Initial list with empty strings...");       foreach (string list in myList) {         ... Read More

How to remove an item from an ArrayList in C#?

Arjun Thakur
Updated on 27-Mar-2020 10:51:33

274 Views

Firstly, set a a new ArrayList and add elements to it.ArrayList arr = new ArrayList(); arr.Add( "Jones" ); arr.Add( "Tom" ); arr.Add( "Henry" );Now let’s remove the item “Tom”. For that, use the Remove() method.arr.Remove("Tom");The following is the complete example to remove an element from ArrayList −Example Live Demousing System; using System.Collections; class Demo {    static void Main(){       ArrayList arr = new ArrayList();       arr.Add( "Jones" );       arr.Add( "Tom" );       arr.Add( "Henry" );       Console.WriteLine("Initial ArrayList...");       foreach(string str in arr) {   ... Read More

How to remove an element from Array List in C#?

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 ArrayList −Example Live Demousing System; using System.Collections; class Demo {    static void Main() {       ArrayList arr = new ArrayList();       arr.Add( "One" );       arr.Add( "Two" );       arr.Add( "Three" );       arr.Add( "Four" );   ... Read More

How to print one dimensional array in reverse order?

Ankith Reddy
Updated on 22-Jun-2020 09:08:24

369 Views

Firstly, declare and initialize one dimensional array.int[] arr = { 35, 12, 66, 90, 34, 2, 64 };Now, use the following to reverse −Array.Reverse(arr);The following is the complete code to reverse a one-dimensional array;Example Live Demousing System; class Demo {    static void Main() {       int[] arr = { 76, 12, 66, 90, 34, 2, 64 };       // Initial Array       Console.WriteLine("Original Array= ");       foreach (int i in arr) {          Console.WriteLine(i);       }       // Reverse Array       Array.Reverse(arr);       Console.WriteLine("Reversed Array= ");       foreach (int j in arr) {          Console.WriteLine(j);       }       Console.ReadLine();    } }OutputOriginal Array= 76 12 66 90 34 2 64 Reversed Array= 64 2 34 90 66 12 76

How to print the first ten Fibonacci numbers using C#?

karthikeya Boyini
Updated on 22-Jun-2020 09:09:04

547 Views

For displaying the first ten numbers, firstly set the first two numbers.int val1 = 0, val2 = 1;Now, use a for loop from 2 to 10, to display first ten Fibonacci numbers −for(i=2;i

How to print duplicate characters in a String using C#?

George John
Updated on 22-Jun-2020 08:53:54

4K+ Views

Set maximum value for char.static int maxCHARS = 256;Now display the duplicate characters in the string.String s = "Welcometomywebsite!"; int []cal = new int[maxCHARS]; calculate(s, cal); for (int i = 0; i < maxCHARS; i++) if(cal[i] > 1) {    Console.WriteLine("Character "+(char)i);    Console.WriteLine("Occurrence = " + cal[i] + " times"); }Above, we have calculated the frequency of characters. The same is shown below in the complete example −Exampleusing System; class Demo {    static int maxCHARS = 256;    static void calculate(String s, int[] cal) {       for (int i = 0; i < ... Read More

How to remove an item from a C# list by using an index?

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; using System.Collections.Generic; using System.Linq; class Program {    static void Main() {       List list1 = new List() {          "Hanks",          "Lawrence",          "Beckham",          "Cooper",       };       ... Read More

How to read inputs as strings in C#?

Chandu yadav
Updated on 22-Jun-2020 08:55:10

7K+ Views

To read inputs as strings in C#, use the Console.ReadLine() method.str = Console.ReadLine();The above will read input as string. You don’t need to use the Convert method here since the input takes string by default.Now display the string entered by user −Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string myStr;       // use ReadLine() to read the entered line       myStr = Console.ReadLine();       // display the line       Console.WriteLine("Result = {0}", myStr);    } }OutputResult =The following is the output. Let’s say the user entered “Amit” as the input −Result = amit

How to convert a list of characters into a string in C#?

karthikeya Boyini
Updated on 22-Jun-2020 08:55:42

184 Views

Firstly, set the characters.char[] arr = new char[5]; arr[0] = 'Y'; arr[1] = 'E'; arr[2] = 'S';Now, convert them into string.string res = new string(arr);The following is the complete code to convert a list of characters into a string −Example Live Demousing System; class Program {    static void Main() {       char[] arr = new char[5];       arr[0] = 'Y';       arr[1] = 'E';       arr[2] = 'S';       // converting to string       string res = new string(arr);       Console.WriteLine(res);    } }OutputYES

How to read inputs as integers in C#?

Arjun Thakur
Updated on 22-Oct-2023 02:02:33

26K+ Views

To read inputs as integers in C#, use the Convert.ToInt32() method.res = Convert.ToInt32(val);Let us see how −The Convert.ToInt32 converts the specified string representation of a number to an equivalent 32-bit signed integer.Firstly, read the console input −string val; val = Console.ReadLine();After reading, convert it to an integer.int res; res = Convert.ToInt32(val);Let us see an example −Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string val;       int res;           Console.WriteLine("Input from user: ");       val = Console.ReadLine();       // convert ... Read More

Advertisements