Primitive Types in C#
Primitive Types in C#
Nausheeda B S
Dept of IT
AIMIT
Data Types in C#
C# Data Types
Reference
Value Types Types
User-
Predefined User- Predefined Defined
Types Defined Types Types
Types
Integer Classes
Boolean Enumerations Objects
Strings Arrays
Characters Structures Delegates
Real Numbers etc. Interfaces
Table of Contents
1. Primitive Data Types
Integer
Floating-Point / Decimal Floating-Point
Boolean
Character
String
Object
2. Declaring and Using Variables
Identifiers, Variables, Literals
3. Nullable Types
3
Primitive Data Types
How Computing Works?
Computers are machines that process data
Data is stored in the computer memory in variables
Variable name
Data type int count = 5;
Variable value
Alphabetical characters: a, b, c, …
9
Integer Types
sbyte (-128 to 127): signed 8-bit
byte (0 to 255): unsigned 8-bit
short (-32,768 to 32,767): signed 16-bit
ushort (0 to 65,535): unsigned 16-bit
int (-2,147,483,648 to 2,147,483,647): signed 32-bit
uint (0 to 4,294,967,295): unsigned 32-bit
long (-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807):
signed 64-bit
ulong (0 to 18,446,744,073,709,551,615): unsigned 64-bit
10
Measuring Time – Example
Depending on the unit of measure we may use different data
types:
14
Floating-Point Types
Floating-point types are:
float (±1.5 × 10−45 to ±3.4 × 1038)
32-bits, precision of 7 digits
double (±5.0 × 10−324 to ±1.7 × 10308)
64-bits, precision of 15-16 digits
The default value of floating-point types:
Is 0.0F for the float type
double a = 1.0f;
double b = 0.33f;
double sum = 1.33f;
bool equal = (a+b == sum); // False!!!
Console.WriteLine("a+b={0} sum={1} equal={2}", a+b, sum, equal);
17
Decimal Floating-Point Types
There is a special decimal floating-point real number type in C#:
decimal (±1,0 × 10-28 to ±7,9 × 1028)
128-bits, precision of 28-29 digits
Used for financial calculations
No round-off errors
21
Boolean Values – Example
Example of boolean variables taking values of true or false:
int a = 1;
int b = 2;
bool greaterAB = (a > b);
Console.WriteLine(greaterAB); // False
bool equalA1 = (a == 1);
Console.WriteLine(equalA1); // True
22
Boolean Type
Character Type
The Character Data Type
The character data type:
Represents symbolic information
26
Character Type
String Type
The String Data Type
The string data type:
Represents a sequence of characters
object dataContainer = 5;
Console.Write("The value of dataContainer is: ");
Console.WriteLine(dataContainer);
dataContainer = "Five";
Console.Write("The value of dataContainer is: ");
Console.WriteLine(dataContainer);
33
Objects
Most Appropriate Data Types
Determine the most appropriate data types:
Person age→ byte
Number of colors (16 million) → int
Bank account balance → decimal
First name → string
Food price → double
Distance between the Sun and Neptune in km → long
Copyright symbol © → char
Check if two names are equal → bool
Introducing Variables
What Is a Variable?
Variables keep data in the computer memory
A variable is a:
Placeholder of information that can be changed at run-time
37
Variable Characteristics
A variable has:
Name
Type (of stored data)
Value
Example:
int counter = 5;
Name: counter
Type: int
Value: 5
38
Declaring and Using Variables
Declaring Variables
When declaring a variable we:
Specify its type
Example:
int height = 200;
40
Identifiers
Identifiers may consist of:
Letters (Unicode)
Digits [0-9]
Underscore "_"
Note:
In C# small letters are considered different
than the capital letters (case sensitivity)
42
Identifiers – Examples
Examples of syntactically correct identifiers:
int New = 2; // here N is capital
int _2Pac; // this identifiers begins with underscore _
string поздрав = "Hello"; // Unicode symbols are acceptable
string greeting = "Hello"; // more appropriate name
int n = 100; // undescriptive
int numberOfClients = 100; // good, descriptive name
int numberOfPrivateClientOfTheFirm = 100; // overdescriptive
The = operator
Holds a variable identifier on the left
E.g. 12345678U, 0U
The 'l' and 'L' suffixes mean a long or ulong
E.g. 9876543L, 0L
53
Integer Literals – Examples
Note: the letter 'l' is easily confused with the digit '1'
So it's better to use 'L'
54
Real Literals
The real literals:
Are used for values of type float, double and decimal
'<value>'
Escaping sequence
57
Escaping Sequences
Escaping sequences are:
Means of presenting a symbol that is usually interpreted
otherwise (like ')
Means of presenting system symbols (like the new line symbol)
63
Nullable Types
Nullable types are instances of the System.Nullable
structure
Wrapper around the primitive types
Nullabe type can represent the normal range of values for its
underlying value type, plus an additional null value
Useful when dealing with databases or other structures that
have default value null
64
Nullable Types – Example
Example with int:
int? someInteger = null;
Console.WriteLine("This is the integer with Null value -> " +
someInteger);
someInteger = 5;
Console.WriteLine("This is the integer with value 5 -> " + someInteger);