Lesson 1 The Basics of CSharp
Lesson 1 The Basics of CSharp
OF
PROGRAMMING
USING
By:
NILDA N. DELA CRUZ, MIT
Course Instructor
Outline:
1. C# Syntax
2. C# Output
3. C# Comments
4. C# Variables
5. C# Data Types
6. C# Type Casting
7. C# User Input
C#
❑ pronounced as “C-Sharp”
❑ object-oriented programming language
created by Microsoft that runs on the
.NET Framework.
❑ roots from the C family, and the language is close
to other popular languages like C++ and Java.
❑ first version was released in year 2002.
The latest version, C# 10, was released
in November 2021.
C# is used for:
✓ Mobile applications
✓ Desktop applications
✓ Web applications
✓ Web services
✓ Web sites
✓ Games
✓ VR
✓ Database applications
✓ And much, much more!
Why Use C#?
To output values or print text in C#, you can use the WriteLine() method:
Console.WriteLine("Hello World!");
Console.WriteLine(“Programming is Easy!");
Console.WriteLine(“If You Study");
Console.WriteLine("It is awesome learning C#");
The Write Method
Comments can be used to explain C# code, and to make it more readable. It can also
be used to prevent execution when testing alternative code.
1. Single-line Comments
Example
// This is a comment
Console.WriteLine("Hello World!");
2. C# Multi-line Comments
Multi-line comments start with /* and ends with */.
Example
Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
1. Names can contain letters, digits and the underscore character (_)
2. Names must begin with a letter
3. Names should start with a lowercase letter and it cannot contain whitespace
4. Names are case sensitive ("myVar" and "myvar" are different variables)
5. Reserved words (like C# keywords, such as int or double) cannot be used as
names
Syntax in declaring variable
Examples:
If you don't want others (or yourself) to overwrite existing values, you
can add the const keyword in front of the variable type.
Example
You can also use the + character to add a variable to another variable:
Note that for numeric values, the + character works as a mathematical operator
Get User Input
In the following example, the user can input his or her username, which is stored in
the variable userName. Then we print the value of userName
Example
// Create a string variable and get user input from the keyboard and
store it in the variable
string userName = Console.ReadLine();
// Print the value of the variable (userName), which will display the
input value
Console.WriteLine("Username is: " + userName);
C# Type Casting
Type casting is when you assign a value of one data type to
another type.
Example
int myInt = 9;
double myDouble = myInt; // Automatic casting: int to
double
Console.WriteLine(myInt); // Outputs 9
Console.WriteLine(myDouble); // Outputs 9
Explicit Casting
Example
It is also possible to convert data types explicitly by using built-in methods, such as
Convert.ToBoolean, Convert.ToDouble, Convert.ToString, Convert.ToInt32
(int) and Convert.ToInt64 (long):
Example
The Console.ReadLine() method returns a string. Therefore, you cannot get information
from another data type, such as int. The following program will cause an error:
Example
Console.WriteLine("Enter your age:");
int age = Console.ReadLine();
Console.WriteLine("Your age is: " + age);
Like the error message says, you cannot implicitly convert type 'string' to 'int'.
Luckily, for you, you just learned from the previous slide (Type Casting), that you can
convert any type explicitly, by using one of the Convert.To methods:
Example
Console.WriteLine("Enter your age:");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Your age is: " + age);