In C#, different data types are registered differently. Furthermore, different actions are allowed to be executed upon them as well. For different data types, a certain amount of memory space is reserved on our computer.

With each data type we define:

  • How to register data in memory
  • The possible values for that data
  • Possible actions on the data

So, let’s talk more about data types in C#.

For the complete navigation of this series check out: C# Back to Basics.

In this article, we are going to cover:

Data Type Registration

Data types that represent the whole numbers are expressed with a certain number of bits. For unsigned numbers, the range is from 0 to 2N-1, and signed numbers range is from -2N-1 to 2N-1-1. So if the data type has a size of 8 bits like the sbyte data type, we can represent its range like this: from -27 to 27-1 => from -128 to 127.

The following table contains different data types that represent the whole numbers:

whole numbers table - C# Data Types

Letter u in front of the type means that type can’t contain negative numbers, it is unsigned.

The types mentioned above are the whole number types. But in C#, we have the number types with the floating point.

We can present them in a table as well:

Decimal numbers table in C# Data Types

In C#, we have two more basic data types:

Char-bool table C# Data Types

To use char type in our code we must place it inside the single quotes: ’a’ or ’A’ or ’3’…

One more type that is often introduced as the basic data type is the string type. But the string is not a value type it is a reference type. To use a string in our code we must place the value inside the double quotes: „This is the string type“ or „3452“…

So, we know we have the value types and reference types, and it is time to talk more about them and variables as well.

Variables in C#

Variable is a name of a memory location in which application stores values.

We should create our variables by following examples:

  • studentName
  • subject
  • work_day …

The wrong examples would be

  • student Name
  • work-day
  • 1place

We should mention that C# is a case-sensitive language so the studentName is not the same as the StudentName.

The C# language has its own set of reserved words, so-called keywords. We can’t use them as a name for our variables. For the list of keywords, you can visit keyword-list.

In C#, we have variables divided into two categories: Value type and Reference type. The difference is that the value type variables stores their values inside their own memory locations, but the memory location for the reference type variables contains only address to the dynamic memory location where the value is stored.

Let’s see how the value types behave in a graphic example:

Value type C# Data Type

Let’s do the same for the reference types:

Reference type C# Data Types

Variable Declarations and Expressions

We should declare our variables in the following way:

<data type> <variable name> ;  or <data type> <variable name>, <variable name> ... ;

So a few examples would be:

Just with the declaration, we can’t assign a value to a value type variables. To do that, we need to use expressions in addition:

<data type> <variable name> = <expression> ;

Again, let’s look at this with an example:

To add a value for the reference type variable, we need to use the newkeyword in the expression part (string is an exception to this rule):

We would like to mention that we don’t recommend to call variables with names „x“ or „y“… We have used that names just for the sake of simplicity. It is a better idea to give meaningful names to our variables.

Conclusion

Now we have learned how to declare our variables and how to assign values to them as well.

In a next post, we are going to talk about operators in C#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.