I intend to introduce you to various types of variables and data types used in C#. You can learn the concepts like Value type, reference type, implicit and explicit variables, and built-in data types with lots of examples.
You can run them in Visual studio or a browser and have fun coding.
A program running will read or update variables. A variable is a storage location in memory with a name.
Table of Contents
Explicit and Implicit variables
An explicitly typed variable will have the format as follows:
<datatype> <variable name> = <value>
An example of implicitly typed variable,
var a = 100; // Compiles as an int
var greet = "Hello Good morning"; //compiles as a string
var b;//Compiler error
b = 's';
The variable ‘a’ and ‘greet’ are initialized at the time of declaration. As a result, the compiler throws an error when we declared the variable ‘b’ and tried to initialize it later.
There are 2 types in C#.
1. Value Type
Value type contains simple types, struct, enumeration and null type. The data type stores the value directly.
a. Simple Types
The simple types are the following.
Reserved word | Aliased type | Range |
sbyte | System.SByte | signed 8-bit integers with values between -128 and 127 |
byte | System.Byte | unsigned 8-bit integers with values between 0 and 255 |
short | System.Int16 | signed 16-bit integers with values between -32768 and 32767 |
ushort | System.UInt16 | unsigned 16-bit integers with values between 0 and 65535 |
int | System.Int32 | signed 32-bit integers with values between -2147483648 and 2147483647 |
uint | System.UInt32 | unsigned 32-bit integers with values between 0 and 4294967295 |
long | System.Int64 | signed 64-bit integers with values between -9223372036854775808 and 9223372036854775807 |
ulong | System.UInt64 | unsigned 64-bit integers with values between 0 and 18446744073709551615 |
char | System.Char | unsigned 16-bit integers with values between 0 and 65535 |
float | System.Single | 32 bit single precision floating point type -3.402823e38 to 3.402823e38 |
double | System.Double | -1.79769313486232e308 to 1.79769313486232e308 |
bool | System.Boolean | 8 bit logical true or false |
decimal | System.Decimal | 128-bit decimal type (+ or -)1.0 x 10e-28 to 7.9 x 10e28 |
An example to use some of them.
float amount = 7.88F;
double preciseAmount = 5.9999999999;
char letter = 'a';
bool isValid = true;
I will continue this tutorial in the next part with more details on enum type and struct type.