C# Language: Variables and consts

In variables and constants we store some data on which we can work, but as the name suggests, they differ in that the variables can be changed and the constants no longer.

1. Variables types

There are different types of variables (and constants), some to store integers, other to store floating-point numbers, some variables store words, and others store only the logical value, true or false.

bool – stores the truth (true) or false (false)

char – a single character written in Unicode

string – string, characters in Unicode

short – 16-bit integer

int – 32-bit integer

long – 64-bit integer

float – a 32-bit floating point number

double – 64-bit floating point number

Below is a program that explains the operation of these variables:

You can also display it in a different way

For sure you understand this, in place {0} will be replace variable value “negation” in place of {1} will be replace variable value “a”, etc.


You can also “convert” the type of double into an int type of type, int type stores only integers so it will display a digit on the screen without numbers after comma

However, if we want to convert the string type with the word “1001” to the type int then convertion do nothing. We will have to use static method.

In case we want to do the opposite, means to replace the int type with string, then we have to use the special ToString() method.

Based on all the information above, try to guess what the following program will display:

If you thought that 1001 and 2346 are congratulations, you’re right because the number 234 was a string and the number 6 are was simply glued to that chain

In addition, we have a ready class “Convert” which has many ready conversion tools, below example:

The above example will display false on the screen

3. Constants

As the name suggests, constants can not change, but they work just like variables. The following example generates an error because we tried to change the value of the “number” constant.

This content also you can find on my steemit blog: https://steemit.com/c-sharp/@slawas/c-language-variables-and-consts

And on medium: https://medium.com/@sawomirkowalski/c-language-variables-and-consts-bae138785626

Thanks and invite you to the next lesson! And I apologize for my English, but I hope that everything is understood, if not, write about it in the comments, or a message to me.

If that post was useful for you share it with your friends :)

Post a comment

Notify about