DEV Community

Cover image for C# Basics - Variables
Grant
Grant

Posted on

C# Basics - Variables

Ok, let's get down to the basics. Like many other coding languages, the basic component is the "variable".

What is a variable?

Think of a variable as a place of storage, where different types of data can be stored. The kinds of data that can be stored in C# and other Obect Orientated language are called Types.

What are Types?

There are many many Types in C# and the .Net Framework, however, we're only going to be looking at the most commonly used ones, with a brief description in its simplest form:

Type Description
string used for writing text
int a whole number value, max 2,147,483,647
long C3
char stores single characters such as 'a' or 'b'
boolean (bool) a true or false value

There are many others obviously, and we'll get to them in future articles.e

What if I want to have a number with a decimal?

There are types specifically used for this purpose. Before I explain this, there is one key element to understand about coding / how computers work. Each data type can only use a certain amount of "bytes". Think of bytes as a unit of information, and that Type can only hold its given amount of bytes, which is why we have multiple ways of storing decimal numbers for example.

Type bytes Description / Example
float 4 bytes Stores fractional numbers. Sufficient for storing 6 to 7 decimal digits
double 8 bytes Stores fractional numbers. Sufficient for storing 15 decimal digits
decimal 16 bytes 28-29 significant digits

As you can see the main difference is how many digits each can hold, which will affect precision and accuracy.

When should I use them?

Decimals are usually used for things like financial/monetary applications, due to their accuracy and precision (due to more decimal places). But as a result, are slower than double and floats.

Double Types are used for real decimal values, except handling money. Their precision isn't great, but when the exact value and rounding is not important they are a great choice.

If you're a mathematician and want to know more about floating-point variables check out floating point info on the MS website.

Examples of these types:

Examples of Types

Writing / naming variables

Variables can be declared in two ways; implicitly and explicitly.

Implicilty - means you tell the application you know there's a variable and it should allocate some space for it, but it should work out the type itself. This is done using the keyword " var "

Explicitly - means you distinctly tell the application you know the intended type and it doesn't need to work it out for itself. It also means if you try to give it a value of something that doesn't match the type, the application will error and break.

Alt Text

Case is Important

When writing your variable names, case is important. Variable names are written in camel case. This is a term you'll see more often whilst learning to code, and is often paired with the term Pascal Case (used for class names - but we'll get to that in our next step).

Camel case is called this due to it's camel hump like casing. Each new word starts with a new uppercase letter, with the first word being lowercase, as shown below:

Alt Text

Next up in this series -> "Classes"

Discussion (0)