C# .Net Variables
Variables are defined bits of memory where temporary data is stored. C# is a strongly typed language, so there are different types of basic variables that can be declared depending on what typed of data you want stored in memory.
See below for C#.net basic built-in variable types, along with amount of memory used.
Type | Description including: Min to Max Values | Memory Usage |
---|---|---|
byte | 0 to 255 | 8 bits (1 byte) |
sbyte | -128 to 127 | |
short | -32,768 to 32,767 | 16 bits (2 bytes) |
ushort | 0 to 65,535 | |
int | -2,147,483,648 to 2,147,483,647 | 32 bits (4 bytes) |
uint | 0 to 4,294,967,295 | |
long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | 64 bits (8 bytes) |
ulong | 0 to 18,446,744,073,709,551,615 | |
float | -3.402823e38 to 3.402823e38 | 32 bits (4 bytes) |
double | -1.79769313486232e308 to 1.79769313486232e308 | 64 bits (8 bytes) |
decimal | -79228162514264337593543950335 to 79228162514264337593543950335 | 128 bits (16 bytes) |
char | A Unicode character. | 16 bits (2 bytes) |
string | A string of Unicode characters. | Dependent on length of string |
bool | True or False. | 8 bits (1 byte) |
object | An object. | Dependent on object |
struct | Used to group related types. | Dependent on variables in struct |
enum | A distinct type that consists of a set of named constants called the enumerator list. | Dependent on length of list |
The below type isn't really a specific type, hence the reason why I separated it from the specific types listed above:
Type | Description |
---|---|
var | var is an implicit type which can alias any type, with no performance issues. It helps to make programs shorter and easier for us to read |
Variables can also be instantiated class to become an object. You can also use the 'var' type for these, or the class name as the type.