Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Sharp

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

C# 

Introduction
What is C#?

• C# is pronounced "C-Sharp".
• It is an object-oriented programming language
created by Microsoft that runs on the .NET
Framework.
• C# 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# 8, was released in September
2019.
C# is uses

• Mobile applications
• Desktop applications
• Web applications
• Web services
• Web sites
• Games
• VR
• Database applications
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# 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.
• Single-line comments start with two forward
slashes (//).
• Any text between // and the end of the line is
ignored by C# (will not be executed).
• This example uses a single-line comment before a
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 (whole numbers), without decimals, such as 123
or -123
• double - stores floating point numbers, with decimals, such as 19.99
or -19.99
• char - stores single characters, such as 'a' or 'B'. Char values are
surrounded by single quotes
• string - stores text, such as "Hello World". String values are
surrounded by double quotes
• bool - stores values with two states: true or false
Constants

• However, you can add


the const keyword if you don't want
others (or yourself) to overwrite
existing values (this will declare the
variable as "constant", which means
unchangeable and read-only)
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 constructing names for
variables
• Names can contain letters, digits and the
underscore character (_)
• Names must begin with a letter
• Names should start with a lowercase letter and it
cannot contain whitespace
• Names are case sensitive ("myVar" and "myvar"
are different variables)
• Reserved words (like C# keywords, such
as int or double) cannot be used as names
Common Data Types
Data Type Size Description
int 4 bytes Stores whole numbers from
-2,147,483,648 to
2,147,483,647
long 8 bytes Stores whole numbers from
-9,223,372,036,854,775,80
8 to
9,223,372,036,854,775,807
float 4 bytes Stores fractional numbers.
Sufficient for storing 6 to 7
decimal digits
Common Data Types
double 8 bytes Stores fractional numbers.
Sufficient for storing 15
decimal digits
bool 1 bit Stores true or false values
char 2 bytes Stores a single
character/letter,
surrounded by single
quotes
string 2 bytes per character Sto
Numbers

• Number types are divided into two groups:


• Integer types stores whole numbers, positive
or negative (such as 123 or -456), without
decimals. Valid types are int and long. Which
type you should use, depends on the numeric
value.
• Floating point types represents numbers with
a fractional part, containing one or more
decimals. Valid types are float and double.
• Even though there are 
Integer Types

• Int
• The int data type can store whole numbers
from -2147483648 to 2147483647. In general,
and in our tutorial, the int data type is the
preferred data type when we create variables
with a numeric value.
• Example
int myNum = 100000;
Console.WriteLine(myNum);
Long

• The 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
with an "L":
• Example
long myNum = 15000000000L;
Console.WriteLine(myNum);
Floating Point Types

• You should use a floating point type whenever


you need a number with a decimal, such as
9.99 or 3.14515.
• Float
• The float data type can store fractional
numbers from 3.4e−038 to 3.4e+038. Note
that you should end the value with an "F":
• Example
float myNum = 5.75F;
Console.WriteLine(myNum);
Double

• The double data type can store fractional


numbers from 1.7e−308 to 1.7e+308. Note
that you can end the value with a "D"
(although not required):
• Example
double myNum = 19.99D;
Console.WriteLine(myNum);
Scientific Numbers

• A floating point number can also be a scientific


number with an "e" to indicate the power of 10:
• Example
• float f1 = 35e3F; double d1 = 12E4D;
Console.WriteLine(f1);
• Console.WriteLine(d1);
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.
• 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# Operators

• Operators are used to perform operations on


variables and values.
• In the example below, we use
the + operator to add together two values:
• Example
int x = 100 + 50;
Arithmetic Operators
Operator Name Description Example

+ Addition Adds x+y


together two
values

- Subtraction Subtracts one x - y


value from
another

* Multiplication Multiplies x*y


two values
Arithmetic Operators
Operator Name Description Example
/ Division Divides one x/y
value by
another
% Modulus Returns the x%y
division
remainder
++ Increment Increases the x++
value of a
variable by 1
-- Decrement Decreases the x--
value of a
variable by 1

You might also like