George John has Published 1167 Articles

Formatted output in C#

George John

George John

Updated on 22-Jun-2020 09:26:01

1K+ Views

To format output in C#, let us see examples to format date and double type.Set formatted output for Double type.Example Live Demousing System; class Demo {    public static void Main(String[] args) {       Console.WriteLine("Three decimal places...");       Console.WriteLine(String.Format("{0:0.000}", 987.383));       Console.WriteLine(String.Format("{0:0.000}", 987.38));   ... Read More

How to print multiple blank lines in C#?

George John

George John

Updated on 22-Jun-2020 09:14:20

608 Views

To display multiple blank lines, we will take a while loop.Here, we are printing 10 blank lines using Console.WriteLine();while (a < 10) {    Console.WriteLine(" ");    a++; }The following is the complete code to display multiple blank lines −Exampleusing System; namespace Program {    public class Demo {   ... Read More

How to find a replace a word in a string in C#?

George John

George John

Updated on 22-Jun-2020 09:04:03

4K+ Views

Firstly, set the string to be replaced.string str = "Demo text!";Now use the replace() method to replace the above string.string res = str.Replace("Demo ", "New ");The following is the complete code to replace a word in a string.Example Live Demousing System; public class Demo {    public static void Main() { ... Read More

Keywords in C#

George John

George John

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

483 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 ... Read More

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

George John

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] ... Read More

How to perform Matrix Addition using C#?

George John

George John

Updated on 22-Jun-2020 08:40:51

771 Views

To perform matrix addition, take two matrices. Enter the rows and columns of matrix one and matrix two. Remember, both the matrix should be a square matrix to add them.Now add elements to both the matrices. Declare a new array and add both the arrays in it.arr3[i, j] = arr1[i, ... Read More

How to call Math Operations using Delegates in C#?

George John

George John

Updated on 22-Jun-2020 08:24:21

228 Views

To understand how to call Math Operations using Delegates in C#, let us see an example wherein we will divide a number.We have a class and a function in it −public class Demo {    public static double DivideFunc(double value) {       return value / 5;    } ... Read More

What is the difference between Read(), ReadKey() and ReadLine() methods in C#?

George John

George John

Updated on 22-Jun-2020 07:44:43

1K+ Views

Read()The Read() reads the next characters from the standard input stream. If a key is pressed on the console, then it would close.int az = Console.Read() Console.WriteLine(z);ReadKey()It reads only a single charactare from the standard input stream.ReadLine()Reads the next line of characters from the standard input stream.Example Live Demousing System; class ... Read More

What is the difference between Read() and ReadLine() methods in C#?

George John

George John

Updated on 22-Jun-2020 07:40:15

539 Views

Read()The Read() reads the next characters from the standard input stream. If a key is pressed on the console, then it would close.int a = Console.Read() Console.WriteLine(a);ReadLine()It reads the next line of characters from the standard input stream.Example Live Demousing System; class Program {    static void Main() {   ... Read More

TrimEnd() method in C#

George John

George John

Updated on 22-Jun-2020 07:37:29

5K+ Views

The TrimEnd() method removes all trailing occurrences of a set of characters specified in an array.For example, the following string has trailing 1s.String str ="234561111".We can easily remove the above trailing 1s from a string using the TrimEnd() method.Let us see an example to remove all trailing 1s.Example Live Demousing System; ... Read More

Advertisements