Found 2587 Articles for Csharp

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

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

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

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

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

325 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

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

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

527 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

Lambda Expressions in C#

karthikeya Boyini
Updated on 22-Jun-2020 08:58:21

703 Views

A lambda expression in C# describes a pattern.Lambda Expressions has the token => in an expression context. This is read as “goes to” operator and used when a lambda expression is declared.Here, we are finding the first occurrence of the element greater than 50 from a list.list.FindIndex(x => x > 50);Above the token => is used. The same is shown below −Example Live Demousing System; using System.Collections.Generic; class Demo {    static void Main() {       List list = new List { 44, 6, 34, 23, 78 };       int res = list.FindIndex(x => x > 50);       Console.WriteLine("Index: "+res);    } }OutputIndex: 4

Keywords in C#

George John
Updated on 22-Jun-2020 08:58:35

688 Views

Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with the @ character.The following are the two types of keywords in C#.Reserved KeywordsabstractasbaseBoolbreakbytecasecatchcharcheckedClassconstcontinuedecimaldefaultdelegatedoDoubleelseenumeventexplicitexternfalseFinallyfixedfloatforforeachgotoifImplicitinin (generic modifier)intinterfaceinternalisLocklongnamespacenewnullobjectoperatorOutout (generic modifier)overrideparamsprivateprotectedpublicReadonlyrefreturnsbytesealedshortsizeofstackallocstaticstringstructswitchthisthrowTruetrytypeofuintulonguncheckedunsafeUshortusingvirtualvoidvolatilewhileContextual Keywordsaddaliasascendingdescendingdynamicfromgetglobalgroupintojoinletorderbypartial (type)partial(method)removeselectset

Large Fibonacci Numbers in C#

Chandu yadav
Updated on 22-Jun-2020 08:33:12

473 Views

To display large Fibonacci numbers, try the following login and code.Here, we have set the value of n as the series. Set it to get the Fibonacci number. Below, we have set it to 100 to get the first 100 Fibonacci numbers.Since the first two numbers in a Fibonacci series are 0 and 1. Therefore, we will set the first two values.int val1 = 0, val2 = 1;The following is the complete code to display large Fibonacci numbers.Example Live Demousing System; public class Demo {    public static void Main(string[] args) {       int val1 = 0, val2 = ... Read More

Advertisements