DATA TYPES,
VARIABLES AND
CONSTANT
Data types, Variables and Constant
C# is a strongly typed language. It means, that you cannot use variable without data types.
Data types tell the compiler that which type of data is used for processing. Such as if you want
to work with string value then you will have to assign string type variable to work with. C#
provides two types of data types: Value types and Reference types.
A Value type data type stores copy of the value whereas the Reference type data types
stores the address of the value. C sharp provides great range of predefined data types but it also
gives the way to create user defined data types.
VALUE TYPES:
Data Types Size Values
sbyte 8 bit -128 to 127
byte 8 bit 0 to 255
short 16 bit -32,768 to 32,767
ushort 16 bit 0 to 65,535
int 32 bit -2,147,483,648 to 2,147,483,647
—SOMEONE FAMOUS
uint 32 bit 0 to 4,294,967,295
long 64 bit -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
VALUE TYPES:
Data Types Size Values
ulong 64 bit 0 to 18,446,744,073,709,551,615
char 16 bit 0 to 65535
float 32 bit -1.5 x 1045 to 3.4 x 1038
double 64 bit -5 x 10324 to 1.7 x 10308
decimal 128 bit -1028 to 7.9 x 1028
bool —SOMEONE
--- FAMOUS True or False
REFERENCE TYPES:
Data Types Size Values
string Variable length 0-2 billion Unicode character
object --- ---
What is a
Variable?
A variable refers to the memory
address. When you create variable,
it creates holds space in the
memory that is used for storing
temporary data. As you know
about c# data types, each data
type has predefined size.
Attributes of
a Variable
A variable has;
- Name
- Address
- Datatype
- Value.
Rules in Naming Variables
01 02
It must begin with a It should not contain any
letter spaces
03 04
It should not be a
Make it descriptive
keyword
Tell whether the following is valid or invalid variable name
1. average Valid
2. last name Invalid
3. num1 Valid
4. 4ever Invalid
5. int Invalid
DECLARING
VARIABLE
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
Example:
DECLARING <datatype> varname = value;
VARIABLE string firstName;
string lastName=”Castillo”;
int num = 0, num2=0;
Example
Create a variable called name of type string and assign it the value "John":
string name = "John";
Console.WriteLine(name);
Console.ReadLine();
TYPES OF VARIABLE
LOCAL VARIABLE GLOBAL VARIABLE CONSTANT VARIABLE
variables declared inside a variables declared outside variables whose values are
method/function a method/function fixed
LET’S DO AN
ALGORTHM
Conversion
C# accepts string value by default. If you are
using other value then you will have to convert of
specific data types.
num1 = Convert.ToInt32(Console.ReadLine());
USING HELPER CLASS
INTEGER int32.parse() or
Convert.ToInt32()
FLOAT
float.Parse()
DOUBLE
Convert.ToDouble()
DECIMAL
Convert.ToDecimal()
BYTE
Convert.ToByte()
THANKS
Do you have any questions?
Please keep this slide for attribution
CREDITS: This presentation template was created by Slidesgo,
including icons by Flaticon, infographics & images by Freepik