inputoutput.md - brainchildservices/curriculum GitHub Wiki
SLIDE-TITLE
C# INPUT AND OUTPUT
SLIDE-1
C# Output
In order to output something in C#, we can use
System.Console.WriteLine() OR
System.Console.Write()
Difference between WriteLine() and Write() method
The main difference between WriteLine() and Write() is that the Write() method only prints the string provided to it, while the WriteLine() method prints the string and moves to the start of next line as well.
SLIDE-2
-
Write()
using System; public class Program { public static void Main() { Console.Write("This is first Console.Write statement."); Console.Write("This is second Console.Write statement."); Console.Write("This is third Console.Write statement."); } }
- Output:(https://dotnetfiddle.net/jv4hg4 )
- Output:(https://dotnetfiddle.net/jv4hg4 )
SLIDE-3
-
WriteLine()
using System; public class Program { public static void Main() { Console.WriteLine("This is first Console.Write statement."); Console.WriteLine("This is second Console.Write statement."); Console.WriteLine("This is third Console.Write statement."); } }
- Output:(https://dotnetfiddle.net/bbe0Pd)
- Output:(https://dotnetfiddle.net/bbe0Pd)
SLIDE-4
C# Input
Console.ReadLine()
(returns a string): Reads a single line from the standard input stream or the command line. As an example, it can be used to ask the user enter their name or age. It reads all the character till we press enter.
Use Int32.Parse to covert input string value to integer value
Use Double.Parse to covert input string value to decimal value etc
Console.ReadKey()
(returns a character): Reads only one single character from the standard input stream or command line. Usually used when you're giving options to the user in the console to select from, such as select A, B or C. Another prominent example, Press Y or n to continue.
Console.Read()
(returns an int): Reads only one single character from the standard input stream. Similar to ReadKey except that it returns an integer. It returns the next character from the input stream, or returns (-1) if there is no more character to be read.
--Console.ReadKey() and Console.Read() won't work in dotnet fiddle
SLIDE-5
Assignment Questions:
Q1. Write a program that prompt user to enter the name. Print the name.
Q2. Write a program that prompt user to enter the age of 5 persons. Calculate the average age and print the result.
Q3. Write a program that prompts user to enter 3 marks(decimal numbers). Add the mark and print the total mark.