You are on page 1of 22

To start the programming in C#.

The C# has been divided into two


types of application.
i) Console Application (Character User Interface)
ii) Windows Application (Graphical User Interface)

What is Console Application?


• A console application is an application that run in a console
window same as a C and C++ language.
• It does not have any graphical user interface, console applications
will have Character User Interface.
• To work with console applications in .Net you have to use a class
called Console that is available within the namespace system
which is the root namespace.
• Console is a class used to work with Input and Output Streams.
• Console class is present in System Namespace.
1. C# code is made up of a series of statements, each of
which is terminated with a semicolon.
2. C# compilers ignore additional spacing in code, whether
it results from spaces, carriage returns, or tab characters
(collectively known as whitespace characters).
3. C# is a block-structured language, meaning statements
are part of a block of code.
For example, a simple block of C# code could take the
following form:
{
<code line 1, statement 1>;
<code line 2, statement 2> ;
<code line 3, statement 2>;
}
4. Blocks of code may be nested inside each other
(that is, blocks may contain other blocks), in which
case nested blocks will be indented further:
{
<code line 1>;
{
<code line 2>;
<code line 3>;
}
<code line 4>;
}
5. C# code are comments
you use /* characters at the start of the comment
and */ characters at the end. These may occur on a
single line, or on different lines, in which case all
lines in between are part of the comment.
6. C# code is that it is case sensitive
Formatted Output with Write(…) and WriteLine(…)
1. For printing long and elaborate series of elements,
special options (also known as overloads) of the
methods Write(…) and WriteLine(…) have been
introduced.
2. These options have a completely different concept than
the standard methods for printing in C#.
3. Their main idea is to adopt a special string, formatted
with special formatting characters and list of values,
which should be substituted in place of “the format
specifiers”.
4. Here is how Write(…) is defined in the standard C#
libraries:
Formatted Output – Examples
The following example prints twice the same thing
but in different ways:
string str = "Hello World!";
// Print (the normal way)
Console.Write(str);
// Print (through formatting string)
Console.Write("{0}", str);
The result of this example execution is:
Hello World!Hello World!
We see as a result "Hello, World!" twice on one line.
This is because there is no printing of a new line in
the program.
First we print the string in a well-known way in
order to see the difference with the other approach.
The second printing is the formatting Write(…) and
the first argument is the format string.
In this case {0} means to put the first argument
after the formatting string in the place of {0}.
The expression {0} is called a placeholder, i.e. a
place that will be replaced by a specific value while
printing.
Example
string name = "John";
int age = 18;
string town = "Seattle";
Console.Write( "{0} is {1} years old from {2}!\n", name, age,
town);
The result of this example execution is as follows:
John is 18 years old from Seattle!
The expression {0} means to put in its place the first
of the arguments submitted after the format string
(in this case name).

Next is {1} which means to replace with the second


of the arguments (age).

The last placeholder is {2}, which means to replace


with the next parameter (town).

Last is \n, which is a special character that indicates


moving to a new line.
class UsingReadLine
{
static void Main()
{
Console.Write("Please enter your first name: ");
string firstName = Console.ReadLine();
Console.Write("Please enter your last name: ");
string lastName = Console.ReadLine();
Console.WriteLine("Hello, {0} {1}!", firstName,
lastName);
}
}
Reading Numbers
static void Main()
{
Console.Write("a = ");
int a = int.Parse(Console.ReadLine());
Console.Write("b = ");
int b = int.Parse(Console.ReadLine());
Console.WriteLine("{0} + {1} = {2}", a, b, a + b);
Console.WriteLine("{0} * {1} = {2}", a, b, a * b);
Console.Write("f = ");
double f = double.Parse(Console.ReadLine());
Console.WriteLine("{0} * {1} / {2} = {3}", a, b, f, a * b / f);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
System.Console.Write("Hello World!");
}
}
}
Two methods for writing the code, which are used extensively
i. Console. Write() – Writes the specified value to the console
window
Ex: Console. Write(“Hello World);

ii. Console. WriteLine() – Add a newline character at the end of


the output
Ex: Console. WriteLine(“Hello World);
Method Description
Write() This method is used to display any message to the user in the
output stream. After displaying the message blinking cursor
remains in the same line.
WriteLine() This method is used to display any message to the user in the
output stream. After displaying the message blinking cursor
moves to a new line.
Read() This method reads the single character and converts that
character into the ASCII value that means it returns the value of
type integer
ReadLine() This will read an input stream from the console window and
return input string when user presses the ENTER key. And it
converts any type of value to string type. Mostly use
ReadLine() instead of Read()
ReadKey() ReadKey() Method makes the program wait for a key press and
it prevents the screen until a key is pressed. In short, it obtains
the next character or any key pressed by the user.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine("Hello World!");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication32
{
class Program
{
static void Main(string[] args)
{
System.Console.Write("Hello World!");
}
}
}

You might also like