C# Basic Syntax, Visual Studio, Console Input - Output
C# Basic Syntax, Visual Studio, Console Input - Output
Saber BHAR
Table of Contents
1. Declaring Variables
2. Reading from the Console
3. Printing to the Console
2
Have a Question?
sli.do
#fund-softuni
3
Introduction and Basic Syntax
C# – Introduction
C# is modern, flexible, general-purpose programming language
Object-oriented by nature, statically-typed, compiled
Create a console
application
6
Writing Code and Running the Program
Start the program from
VS using [Ctrl + F5]
7
Declaring Variables
Declaring Variables
To declare variables in C# you need to use the pattern:
{data type / var} {variable name} = {value};
Examples:
var firstNumber = 5; int firstNumber = 5;
var name = "Pesho"; string name = "Pesho";
var isPassed = false; bool isPassed = false;
var gender = 'F'; char gender = 'F';
var mathGrade = 5.49; double mathGrade = 5.49;
Type is inferred from the right side of the expression (use var) 9
Console I/O
Reading from and Writing to the Console
Reading from the Console
We can read/write to the console, using the Console class
Use the System namespace to access System.Console class
using System;
11
Converting Input from the Console
Console.ReadLine() returns a string
Convert the string to number by parsing:
12
Printing to the Console
We can print to the console, using the Console class
Use the System namespace to access System.Console class
13
Printing on the Same Line
Sometimes, we want to print text on the same line
Use Console.Write():
Console.Write("Name: ");
var name = Console.ReadLine();
Console.WriteLine("Hi, " + name);
14
Printing on the Console
Use string concatenation to print text with numbers
Or the {0} placeholders
Or the ${variable} syntax
using System;
17
Using Placeholders
Using placeholders to print at the console:
Placeholder {0}
corresponds to name
Placeholder {0}
corresponds to name
var name = "Gosho";
var age = 5;
Console.WriteLine("Name: {0}, Age: {1}", name, age);
18
Problem: Add Two Numbers
Write a C# program to read two integers and add them together.
Print the sum like shown at the examples:
2
2 + 5 = 7
5
1
3 1 + 3 = 4
-3
5 -3 + 5 = 2
19
Solution: Add Two Numbers
Read the integers from the console, sum and print them:
var a = int.Parse(Console.ReadLine());
var b = int.Parse(Console.ReadLine());
var sum = a + b;
21
Formatting Numbers in Placeholders
D – format number to certain digits with leading zeros
F – format floating point number with certain digits after the
decimal point
Examples:
22
Problem: Employee Data
Write a C# program, which reads employee information and
prints them, formatted like shown below:
23
Solution: Employee Data
Read the data from the console and format it:
var name = Console.ReadLine();
var age = int.Parse(Console.ReadLine());
var employeeId = int.Parse(Console.ReadLine());
var salary = double.Parse(Console.ReadLine());
Console.WriteLine($"Name: {name}");
Console.WriteLine($"Age: {age}");
Console.WriteLine($"Employee ID: {employeeId:D8}");
Console.WriteLine($"Salary: {salary:F2}");
Check your solution here: https://judge.softuni.bg/Contests/559
24
C# Basic Syntax
Live Exercises in Class (Lab)
25
Summary
Declare variables is C# using var
Read input from the console using
Console.ReadLine() string
Convert input to numbers by parsing, e.g.
int.Parse(str), double.Parse(str)
Print to the console using Console.Write()
and Console.WriteLine()
Use concatenation +, placeholders {0} and string
interpolation $"text {variable}" 26