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

C# Basic Syntax, Visual Studio, Console Input - Output

This document provides an introduction to C# basics including syntax, variables, and console input/output. It discusses declaring variables, reading input from the console using Console.ReadLine(), and printing output to the console using Console.WriteLine(). It provides examples of declaring different variable types, parsing user input to numbers, and formatting output strings.

Uploaded by

saber
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

C# Basic Syntax, Visual Studio, Console Input - Output

This document provides an introduction to C# basics including syntax, variables, and console input/output. It discusses declaring variables, reading input from the console using Console.ReadLine(), and printing output to the console using Console.WriteLine(). It provides examples of declaring different variable types, parsing user input to numbers, and formatting output strings.

Uploaded by

saber
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

C# – Introduction

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

 Runs on .NET Framework / .NET Core

static void Main()


{
Console.Write("What's your name? ");
var name = Console.ReadLine();
Console.WriteLine($"Hello, {name}!");
}
5
Using Visual Studio
 Visual Studio (VS)
is powerful IDE
for C# and other
languages

 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;

 Reading input from the console using Console.ReadLine():

var name = Console.ReadLine();


Returns string

11
Converting Input from the Console
 Console.ReadLine() returns a string
 Convert the string to number by parsing:

var name = Console.ReadLine();


var age = int.Parse(Console.ReadLine());
var salary = double.Parse(Console.ReadLine());

12
Printing to the Console
 We can print to the console, using the Console class
 Use the System namespace to access System.Console class

 Writing output to the console using Console.WriteLine():

var name = "Gosho"; Prints Gosho


Console.WriteLine(name);

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

var name = "Gosho";


var age = 5;
Console.WriteLine("Name: " + name + ", Age: " + age);
Console.WriteLine("Name: {0}, Age: {1}", name, age);
Console.WriteLine($"Name: {name}, Age: {age}");
15
Problem: Greeting
 Write a C# program, which greets the user by name:

Pesho Hello, Pesho!

Ivan Hello, Ivan!

Merry Hello, Merry!

Check your solution here: https://judge.softuni.bg/Contests/559


16
Solution: Greeting
 Read name from the console and print it:

using System;

static void Main()


{
var name = Console.ReadLine();
Console.WriteLine("Hello, " + name + "!");
}

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;

Console.WriteLine("{0} + {1} = {2}", a, b, sum);

Check your solution here: https://judge.softuni.bg/Contests/559


20
Using String Interpolation
 Using string interpolation to print at the console:

Put $ in front of "" to use


var name = "Gosho"; string interpolation
var age = 5;
Console.WriteLine($"Name: {name}, Age: {age}");

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:

var grade = 5.5334;


var percentage = 55;
Console.WriteLine("{0:F2}", grade); // 5.53
Console.WriteLine("{0:D3}", percentage); // 055

22
Problem: Employee Data
 Write a C# program, which reads employee information and
prints them, formatted like shown below:

Ivan Name: Ivan


24 Age: 24
1192 Employee ID: 00001192
1500.353 Salary: 1500.35

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

You might also like