Lesson 1 INTRODUCTION TO C# LANGUAGE.pdf

JLMagdaraog 20 views 49 slides Oct 17, 2024
Slide 1
Slide 1 of 49
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49

About This Presentation

Computer Programming C#


Slide Content

INTRODUCTION
TO C# LANGUAGE
Prepared by: Ms. Maricel M. Prongo

LEARNING OBJECTIVES
In this lesson, you should be able to:
know what a variable, constant, and literal are;
use variables, constants, and literals in
creating programs;
identify and assign the correct data type to a
variable; and
construct statements and code blocks using
Visual C#.

WHAT IS C# LANGUAGE?
is a programming language developed by
Microsoft that runs on the .NET
Framework.
is used develop web apps, desktop apps,
mobile apps, games and much more.

WHAT IS C# LANGUAGE?
is pronounced as "C - Sharp".
has roots from the C family, and the language
is close to other popular languages like C++
and Java.
the first version was released in year 2002.
The latest version, C# 11, was released in
November 2022.

C# is used for
mobile applications
desktop applications
web applications
web services
web sites
games
database applications
and much, much more!

WHAT IS IDE?
a software used by a developer so that they
can easily code in a certain programming
language, IDE has tons of features that
make's programming easier.
INTEGRATED DEVELOPMENT ENVIRONMENT

C# LANGUAGE SYNTAX
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}

C# OUTPUT
to output values or print text in C#, you can
use the WriteLine() method.
you can add as many WriteLine() methods as
you want. Note that it will add a new line for
each method.

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Console.WriteLine("I am Learning C#");
Console.WriteLine("It is awesome!");
}
}
}

EXAMPLE OUTPUT:
Hello World!
I am Learning C#
It is awesome!

C# OUTPUT
you can also output numbers, and perform
mathematical calculations.
Console.WriteLine(3 + 3);
Output:
6

C# OUTPUT
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.

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello World! ");
Console.Write("I will print on the same line.");
}
}
}

EXAMPLE OUTPUT:
Hello World! I will print on the same line.

SEATWORK #2.1
Create a program that will display
basic information about yourself
using basic programming
constructs and new lines.
M
S
P
R
O
N
G
O

COMMENTS
can be used to explain C# code, and
make it more readable. It can also be
used to prevent execution when
testing alternative code.
can be singled-lined or multi-lined

SINGLE - LINE
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
// This is a comment
Console.WriteLine("Hello World!");
}
}
}

MULTI - LINE
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
/* The code below will print the words
Hello World to the screen, and it is amazing */
Console.WriteLine("Hello World!");
}
}
}

SINGLE OR
MULTI LINE ?

C# VARIABLES
are containers for storing data values.
In C#, there are different types of
variables:
int - stores integers
double - store floating point numbers
char - stores single character
string - stores text
bool - stores values with two states.

C# DATA TYPE
it specifies the size and type of variables
values.
it is important to use the correct data type
for corresponding variable; to avoid errors,
to save time and memory, but it will also
make your code more maintainable and
readable.

C# DATA TYPE

INTEGER TYPES
it stores whole numbers, positive
or negative (such as 123 or -456),
without decimals.
valid types are int and long.

INTEGER TYPES
int
data type can store whole numbers
from -2147483648 to 2147483647.
it is preferred data type when we
create variables with numeric value.

INTEGER TYPES
long
data type can store whole numbers
from -9223372036854775808 to
9223372036854775807.
This is used when int is not large
enough to store the value.
Note that you should end the value in an
"L".

FLOATING POINTS
you should use floating point type whenever you need
a number with decimal, such as 9.99 or 3.14515.
the float and double data type can store fractional
numbers.
Note that you should end the value with an "F" for
floats and "D" for doubles.

FLOAT EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
float myNum = 5.75F;
Console.WriteLine(myNum);
}
}
}

DOUBLE EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
double myNum = 19.99D;
Console.WriteLine(myNum);
}
}
}

USE FLOAT
OR DOUBLE?

BOOLEANS
a boolean data type is declared
with the bool keyword and can only
take the values true or false.
mostly used for conditional testing.

CHARACTERS
the char data type is used to store
a single character.
The char must be surrounded by
single quotes, like 'A' or 'c'.

STRINGS
the string data type is used to store
a sequence of characters (text).
String values must be surrounded
by double quotes.

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
string greeting = "Hello World";
Console.WriteLine(greeting);
}
}
}

C# DISPLAY
VARIABLES

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.

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
string name = "Jasmine";
Console.WriteLine("Hello " + name);
}
}
}

DISPLAY VARIABLES
You can also use the +
character to add a variable to
another variable.

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
string firstName = "Jasmine ";
string lastName = "Calupad";
string fullName = firstName + lastName;
Console.WriteLine(fullName);
}
}
}

DISPLAY VARIABLES
For numeric values, the + character
works as a mathematical operator
(notice that we use int (integer)
variables here)

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 5;
int y = 6;
Console.WriteLine(x + y);
}
}
}

C# MULTIPLE
VARIABLES

DECLARE MANY VARIABLES
To declare more than one variable
of the same type, use a comma-
separated list.

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x = 5, y = 6, z = 50;
Console.WriteLine(x + y + z);
}
}
}

DECLARE MANY VARIABLES
You can also assign the same
value to multiple variables in one
line.

EXAMPLE:
using System;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int x, y, z;
x = y = z = 50;
Console.WriteLine(x + y + z);
}
}
}

C# IDENTIFIERS

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

EXAMPLE:
// Good
int minutesPerHour = 60;
/* OK, but not so easy to understand what
m actually is*/
int m = 60;

THE GENERAL RULES FOR NAMING VARIABLES ARE:
Names can contain letters, digits and underscores
Names must begin with a letter or an underscore (_)
Names are case sensitive (myVar and myvar are different variables)
Names cannot contain whitespaces or special characters like !, #, %,
etc.
Reserved words (such as int) cannot be used as names
M
S
P
R
O
N
G
O