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 computers.

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#.

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!

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

In this article, we are going to cover:

Data Type Registration in C#

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 the 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 the 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.

But if for some reason we want to name our variable with a keyword, to avoid clashes, we can use the @ sign in front of the name:

int @int; //valid
string @return; //valid
decimal decimal; //not valid

Contextual Keywords

The contextual keywords are the ones that we can use for the variable names but without using the @ sign in front:

add alias ascending async await by descending dynamic equals from get global group in into join
let nameof on orderby partial remove select set unmanaged value var when where yield

Value and Reference Types

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 in C#

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:

class Program
{
    static void Main(string[] args)
    {
        int age;
        double temperature, change;
        Student student;
    }
}

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

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

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

class Program
{
    static void Main(string[] args)
    {
        int x = 5;
        int y = 145 + x;
        char p = 'p';
        p = 'A';
    }
}

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

class Program
{
    static void Main(string[] args)
    {
        Student student = new Student("John", 25);
    }
}

We would like to mention that we don’t recommend calling variables with names „x“ or „y“… We have used those 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 the next post, we are going to talk about operators in C#.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!