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

Lesson 1 The Basics of CSharp

The document provides an introduction to the C# programming language. It covers C# syntax, output, comments, variables, data types, type casting, and getting user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lesson 1 The Basics of CSharp

The document provides an introduction to the C# programming language. It covers C# syntax, output, comments, variables, data types, type casting, and getting user input.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

FUNDAMENTALS

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#?

❑It is one of the most popular programming


language in the world
❑It is easy to learn and simple to use
❑It has a huge community support
❑C# is an object oriented language which gives
a clear structure to programs and allows code
to be reused, lowering development costs
❑As C# is close to C, C++ and Java, it makes it
easy for programmers to switch to C# or vice
versa
C# IDE

❑ The easiest way to get started with C#, is to


use an IDE.
❑ An IDE (Integrated Development Environment)
is used to edit and compile code.
https://www.programiz.com/csharp-programming/online-compiler/
https://www.onlinegdb.com/online_csharp_compiler
C# Syntax
C# Output

To output values or print text in C#, you can use the WriteLine() method:

Console.WriteLine("Hello World!");

You can add as many WriteLine() methods as you want. Note


that it will add a new line for each method:

Console.WriteLine(“Programming is Easy!");
Console.WriteLine(“If You Study");
Console.WriteLine("It is awesome learning C#");
The Write Method

There is also a Write() method, which is similar to WriteLine().The


only difference is that it does not insert a new line at the end of
the output:

Console.Write(“This statement! ");


Console.Write("will print on the same line.");
C# Comments

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

Single-line comments start with two forward slashes (//).

Example
// This is a comment
Console.WriteLine("Hello World!");
2. C# Multi-line Comments
Multi-line comments start with /* and ends with */.

Example

/* The code below will print the words Hello


World to the screen, and it is amazing */
Console.WriteLine("Hello World!");
C# Variables

Variables are containers for storing data values.

In C#, there are different types of variables (defined with different


keywords), for example:

int - stores integers


float – stores floating point numbers up to 6 or 7 precisions
double - stores floating point numbers up to 15 precisions
char - stores single characters, surrounded by single quotes
string - stores text. String values are surrounded by double quotes
bool - stores values with two states: true or false
C# Identifiers

All C# variables must be identified with unique names.

These unique names are called identifiers.

Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).

The general rules for naming variables are:

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

type variableName = value;

Examples:

int myNum = 5; // Integer (whole number)


double myDoubleNum = 5.99D; // Floating point number
char myLetter = 'D'; // Character
bool myBool = true; // Boolean
string myText = "Hello"; // String
C# Constants

If you don't want others (or yourself) to overwrite existing values, you
can add the const keyword in front of the variable type.

This will declare the variable as "constant", which means unchangeable


and read-only:

Example

const int myNum = 15;


C# Display Variables
The WriteLine() method is often used to display variable values to the console
window.

To combine both text and a variable, use the + character:

string name = "John";


Console.WriteLine("Hello " + name);

You can also use the + character to add a variable to another variable:

string firstName = "John ";


string lastName = "Doe";
string fullName = firstName + lastName;
Console.WriteLine(fullName);

Note that for numeric values, the + character works as a mathematical operator
Get User Input

You have already learned that Console.WriteLine() is used to output (print)


values. Now we will use Console.ReadLine() to 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

// Type your username and press enter


Console.WriteLine("Enter username:");

// 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.

In C#, there are two types of casting:

Implicit Casting (automatically) - converting a smaller type


to a larger type size
char -> int -> long -> float -> double

Explicit Casting (manually) - converting a larger type to a


smaller size type
double -> float -> long -> int -> char
Implicit Casting

Implicit casting is done automatically when passing a smaller size type to a


larger size 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

Explicit casting must be done manually by placing the type in


parentheses in front of the value:

Example

double myDouble = 9.78;


int myInt = (int) myDouble; // Manual casting: double to int

Console.WriteLine(myDouble); // Outputs 9.78


Console.WriteLine(myInt); // Outputs 9
Type Conversion Methods

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

int myInt = 10;


double myDouble = 5.25;
bool myBool = true;

Console.WriteLine(Convert.ToString(myInt)); // convert int to string


Console.WriteLine(Convert.ToDouble(myInt)); // convert int to double
Console.WriteLine(Convert.ToInt32(myDouble)); // convert double to int
Console.WriteLine(Convert.ToString(myBool)); // convert bool to string
User Input and Numbers

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

The error message will be something like this:

Cannot implicitly convert type 'string' to 'int'

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

You might also like