Variables and Simple built-in Types in C#

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.

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 wordAliased typeRange
sbyteSystem.SBytesigned 8-bit integers with values between -128 and 127
byteSystem.Byteunsigned 8-bit integers with values between 0 and 255
shortSystem.Int16 signed 16-bit integers with values between -32768 and 32767
ushortSystem.UInt16unsigned 16-bit integers with values between 0 and 65535
intSystem.Int32signed 32-bit integers with values between -2147483648 and 2147483647
uintSystem.UInt32 unsigned 32-bit integers with values between 0 and 4294967295  
longSystem.Int64signed 64-bit integers with values between -9223372036854775808 and 9223372036854775807
ulongSystem.UInt64unsigned 64-bit integers with values between 0 and 18446744073709551615
charSystem.Charunsigned 16-bit integers with values between 0 and 65535
floatSystem.Single32 bit single precision floating point type -3.402823e38 to 3.402823e38
doubleSystem.Double-1.79769313486232e308 to 1.79769313486232e308  
boolSystem.Boolean8 bit logical true or false
decimalSystem.Decimal128-bit decimal type (+ or -)1.0 x 10e-28 to 7.9 x 10e28
C# simple types

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.

Prev: Introduction to C# and Hello world program

Next: Enum, struct, nullable types