Post

Primitive Data Types

Primitive Data Types

  • Primitive data types are the main built-in types, and could be used to build other data types.
TypeDescriptionDeclarationNaming Conventions
VariablesA name given to a storage location in memoryint number;
int Number = 1;
CamelCase
ConstantsAn immutable value (cannot be changed)const float pi = 3.14f;PascalCase
Real NumbersTo declare numerical valuefloat number = 1.2f;
decimal number = 1.2m;
CamelCase

Primitive Types:

TypeC# Type.NET TypeBytesRange
Integral NumbersbyteByte10 to 255
 shortInt162-32,768 to 32,767
 intInt324-2.1B to 2.1B
 longInt648
Real NumbersfloatSingle4-3.4 x 10-38 to 3.4 x 10-38
 doubleDouble8
 decimalDecimal16-7.9x10-28 to 7.9x10-28
CharactercharChar2Unicode Characters
BooleanboolBoolean1True/False

Common Terms

  1. Overflowing
    • Overflow is an operation that occurs when a calculation produces a result that is greater in magnitude than that which a given register or storage location can store or represent.
  2. Scope
    • Scope is where a constant or variable has meaning.

Type Conversion:

  • A conversion of data type can be performed based on type compatibility and data compatibilty.
Conversion TypeDescriptionExample
Implicit ConversionThe compiler will make conversionint i = 1;
Int b = i;
Explicit ConversionWe ask the compiler to convert the data typeint i = 1;
byte b = (byte) i;
Non-Compatible ConversionSome data types cannot be converted into one anothervar stringvariable = “1234”;
byte b = Convert.ToByte(stringvariable);

Type Conversion Methods

| Method | Description | Syntax | | —— | ———– | —— | | Parsing | Used to convert string type date to primitive value type. | Console.Write(“Enter any number : “);
var number = int.Parse(Console.ReadLine());
Console.WriteLine($”You have entered : {number}”); | | ConvertClass | Used to convert one primitive type to another type. | string num = “23”;
int number = Convert.ToInt32(num); | | Explicit Cast Operator() | Used with non-primitive types to up level or down level casting | int num1, num2;
float avg;
num1 = 10;
num2 = 21;
avg = (float)(num1 + num2) / 2;
Console.WriteLine($”average is : {avg}”); |

Data Types - Value and Reference Type

Value TypeReference Type
Fixed in sizeNot fixed in size
Value types are stored in stackReference types are stored in managed heap
Actual values of data are stored in stackA reference to the heap is stored in stack
If you assign a value of a variable to another, it will create two copies.If you assign a value of a varaible to another, it will refer to the same copy
Example: All Primitive Data types (except string and object), struct and enumExample: string, object, class, interface and delegate

Operators

| Operator Type | Operator | Syntax | | ————- | ——– | —— | | Arithmetic Operators | Add + | a + b | | | Subtract - | a - b | | | Multiply * | a * b | | | Divide / | a / b | | | Remainder % | a % b | | | Increment ++ | a++ | | | Decrement – | a– | | Comparison Operators | Equal == | a == b | | | Not Equal != | a != b | | | Greater than > | a > b | | | Greater than or equal to >= | a >= b | | | Less than < | a < b | | | Less than or equal to <= | a <= b | | Assignment Operators | Assignment = | a = 2 | | | Addition Assignment += | a += 3 is same as a = a + 3 | | | Subtraction Assignment -= | a -= 3 is same as a = a - 3 | | | Multiplication Assignment *= | a *= 3 is same as a = a * 3 | | | Division Assignment /= | a /= 3 is same as a = a / 3 | | Logical Operators | And && | a < 5 && b > 2 | | | Or || | a < 5 || b > 2 | | | Not ! | !(a < 5 && b > 2) | | Bitwise Operators | And & | (a & b) | | | Or | | (a | b) | | | XOR ^ | (a ^ b) | | | Complement ~ | (~a ) | | | Left Shift « | (a « 2)| | | Right Shift » | (a » 2) |

This post is licensed under CC BY 4.0 by the author.