Lesson7 Basic Input .Output
Lesson7 Basic Input .Output
By
BAZZEKETA DATSUN
MIT(MUK)
Tel: 0705333525
Email: datsunbazzeketa@yahoo.com
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,
// 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.