0% found this document useful (0 votes)
31 views

Lesson7 Basic Input .Output

The document provides information on input and output in C# using the Console class. It discusses using Console.WriteLine() and Console.Write() to output text, and Console.ReadLine() to get input from the user as a string. It also covers converting string input to other data types like int and double using methods like Convert.ToInt32().
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Lesson7 Basic Input .Output

The document provides information on input and output in C# using the Console class. It discusses using Console.WriteLine() and Console.Write() to output text, and Console.ReadLine() to get input from the user as a string. It also covers converting string input to other data types like int and double using methods like Convert.ToInt32().
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Prepared

By
BAZZEKETA DATSUN
MIT(MUK)
Tel: 0705333525
Email: [email protected]

By Bazzeketa Datsun 1
 In order to output something in C#, we can
use

 System.Console.WriteLine()
 OR
 System.Console.Write()

 Explanation
◦ System is a namespace,
◦ Console is a class within namespace System
◦ WriteLine and Write are methods of class Console
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication
 {
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine("C# is cool");
 Console.ReadKey();
 }}}
 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.
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication
 {
 class Program
 {
 static void Main(string[] args)
 {
 Console.WriteLine("Prints on ");
 Console.WriteLine("New line");

 Console.Write("Prints on ");
 Console.Write("Same line");
 }}}
 A better alternative for printing concatenated
string is using formatted string.
 Formatted string allows programmer to use
placeholders for variables. For example,

 The following line,


 Console.WriteLine("Value = " + val);

 can be replaced by,


 Console.WriteLine("Value = {0}", val);
◦ {0} is the placeholder for variable val which will be
replaced by value of val. Since only one variable is
used so there is only one placeholder.
 Multiple variables can be used in the formatted string.
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication
 {
 class Program
 {
 static void Main(string[] args)
 {
 int firstNumber = 5, secondNumber = 10, result;
 result = firstNumber + secondNumber;
 Console.WriteLine("{0} + {1} = {2}", firstNumber, secondNumber,
result);
 }}}
 In the code on the previous slide,

 {0} is replaced by firstNumber,


 {1} is replaced by secondNumber and
 {2} is replaced by result.

 This approach of printing output is more


readable and less error prone than using +
operator.
 InC#, the simplest method to get input
from the user is by using the ReadLine()
method of the Console class.

 See example on the next slide


 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication {
 class Program {
 static void Main(string[] args) {
 // Type your username and press enter
 Console.WriteLine("Enter username:");

 // Create a string variable and get user input from the keyboard and store it
 string userName = Console.ReadLine();

 // Print the value of the variable (userName), which will display the input value
 Console.WriteLine("Username is: " + userName);
 }}}
 The Console.ReadLine() method returns
a string.

 Therefore,
you cannot get information
from another data type, such as int.

 Theprogram on the next slide will cause


an error:
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication {
 class Program {
 static void Main(string[] args)
 {
 Console.WriteLine("Enter your age:");
 int age = Console.ReadLine();
 Console.WriteLine("Your age is: " + age);
 }
 }}

You are getting an error because Console.ReadLine() turns your


input into a string, yet you created your datatype as an interger.
 Reading numeric values is tricky in C#.
 We still use the same ReadLine() method but
since the ReadLine() method receives the input as
string, it needs to be converted into integer or
floating point type.

 Converting strings to other datatypes requires


using some methods
◦ Convert.ToInt32(userInput). This changes a
string to an interger
◦ Convert.ToDouble(userInput); This changes a
string to a double
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication {
 class Program {
 static void Main(string[] args)
 {
 Console.WriteLine("Enter your age:");

 int age = Convert.ToInt32(Console.ReadLine());

 Console.WriteLine("Your age is: " + age);}


 }}
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication {
 class Program {
 static void Main(string[] args)
 {
 String y;
 Int age;
 Console.WriteLine("Enter your age:");
 y=Console.ReadLine(); //capture the input as a string
 int age = Convert.ToInt32(y); //convert the input to an interger
 Console.WriteLine("Your age is: " + age);
 }
 }}
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 namespace DemoApplication {
 class Program {
 static void Main(string[] args)
 {
 String y;
 double age;
 Console.WriteLine("Enter your age:");
 y=Console.ReadLine(); //capture the input as a string
 double age = Convert.ToInt32(y); //convert the input to a double
 Console.WriteLine("Your age is: " + age);
 }
 }}

You might also like