Prepared
By
BAZZEKETA DATSUN
MIT(MUK)
Tel: 0705333525
Email: datsunbazzeketa@[Link]
By Bazzeketa Datsun 1
In order to output something in C#, we can
use
[Link]()
OR
[Link]()
Explanation
◦ System is a namespace,
◦ Console is a class within namespace System
◦ WriteLine and Write are methods of class Console
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
[Link]("C# is cool");
[Link]();
}}}
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 [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
[Link]("Prints on ");
[Link]("New line");
[Link]("Prints on ");
[Link]("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,
[Link]("Value = " + val);
can be replaced by,
[Link]("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 [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication
{
class Program
{
static void Main(string[] args)
{
int firstNumber = 5, secondNumber = 10, result;
result = firstNumber + secondNumber;
[Link]("{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 [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication {
class Program {
static void Main(string[] args) {
// Type your username and press enter
[Link]("Enter username:");
// Create a string variable and get user input from the keyboard and store it
string userName = [Link]();
// Print the value of the variable (userName), which will display the input value
[Link]("Username is: " + userName);
}}}
The [Link]() 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 [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication {
class Program {
static void Main(string[] args)
{
[Link]("Enter your age:");
int age = [Link]();
[Link]("Your age is: " + age);
}
}}
You are getting an error because [Link]() 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
◦ [Link](userInput); This changes a
string to a double
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication {
class Program {
static void Main(string[] args)
{
[Link]("Enter your age:");
int age = Convert.ToInt32([Link]());
[Link]("Your age is: " + age);}
}}
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication {
class Program {
static void Main(string[] args)
{
String y;
Int age;
[Link]("Enter your age:");
y=[Link](); //capture the input as a string
int age = Convert.ToInt32(y); //convert the input to an interger
[Link]("Your age is: " + age);
}
}}
using System;
using [Link];
using [Link];
using [Link];
using [Link];
namespace DemoApplication {
class Program {
static void Main(string[] args)
{
String y;
double age;
[Link]("Enter your age:");
y=[Link](); //capture the input as a string
double age = Convert.ToInt32(y); //convert the input to a double
[Link]("Your age is: " + age);
}
}}