Found 2628 Articles for Csharp

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

546 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

How to read a line from the console in C#?

Samual Sam
Updated on 22-Jun-2020 08:56:44

1K+ Views

The ReadLine() method is used to read a line from the console in C#.str = Console.ReadLine();The above will set the line in the variable str.Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       string str;       // use ReadLine() to read the entered line       str = Console.ReadLine();       // display the line       Console.WriteLine("Input = {0}", str);    } }OutputInput =Above, we displayed a line using the Console.ReadLine() method. The string is entered by the user from the command line.

Lifecycle and States of a Thread in C#

Ankith Reddy
Updated on 22-Jun-2020 08:57:53

347 Views

Threads are lightweight processes. Each thread defines a unique flow of control. The life cycle of a thread starts when an object of the System.Threading.Thread class is created and ends when the thread is terminated or completes execution.Here are the various states in the life cycle of a thread −The Unstarted StateIt is the situation when the instance of the thread is created but the Start method is not called.The Ready StateIt is the situation when the thread is ready to run and waiting CPU cycle.The Not Runnable StateA thread is not executable, whenSleep method has been calledWait method has been ... Read More

Advertisements