Last Updated:

Data Types and Creating a Variable in C# (sharp)

"Data Types and Creating a Variable in C# (sharp)" – let's find out!

A variable is an area of memory that a developer works with by assigning certain values to it.

A data type is an explanatory note to the compiler from a developer about how much memory should be allocated for the data used by the developer.

Assigning a value means putting something, putting it in a variable.

In C#, there are many different types provided by the rich built-in library, but most of them are specialized types that are used to implement specific tasks. The scope of application of such special types is quite narrow, so in this lesson we will get acquainted with the standard types that are used everywhere and everywhere. We will get acquainted with special types in the following lessons.

Standard Data Types


Standard data Type of C# Sharp

There are a lot of standard types, aren't there? And there are even more special types...

It makes no sense to memorize this table (in the course of practice you will memorize them as your own name due to the frequency of use), but it makes sense to have this table at hand. Save this page to your bookmarks and you will always have a small reference book at hand!

As it is not difficult to notice, opposite each type is its weight in bits (the unit of measurement of information). If you have created a variable of a certain type, it occupies a specified amount of memory in the computer. Each type has its own range of valid values, that is, what is the maximum/minimum possible value can be put in a variable with a given type.

All types that have fractional values in the range (for example, 5.0 or 7.9) support assigning a fractional (not integer) value to a variable. These types are also called "floating-point type".

The Truth About Types or How to Become Great

When writing programs, be vigilant, choose a type that will require as little memory as possible. In medium-sized projects, there can be millions of variables.
Let's think: why save? The computer is powerful, there is a lot of memory, I will choose the most voluminous type!

Take the decimal type (128 bits -> 16 bytes) and multiply this number by one million (the number of variables), we get 16 * 1000000 = 16000000 bytes -> 15.26 megabytes. That's how much only your variables will take, and you still need space for various processes and actions of the compiler itself. To put it mildly, the program begins to eat a lot of memory and as a result has a greater chance to "slow down" in the course of its work. And if we could do with the int type (32 bits -> 4 bytes) in the whole program, then we get 4 * 1000000 = 4000000 bytes -> 3.81 megabytes. 15.26 vs. 3.81 is a tangible difference, isn't it? Also attach here the memory occupied by other programs on your computer and such figures are obtained...

If you want to become a real programmer in a large and developing company in the future, then approach wisely to the choice of the type of variable, this is very important.

Creating a Variable in C# (sharp)


In the C# language, what you call the variable plays a very important role. The compiler looks at the very meaning of the name by a large drum, but he looks at the case of letters (uppercase or lowercase) especially carefully. That is, the variables with the names "Var1" and "var1" for the compiler are completely different, and this is due to the fact that the first letter in the first case is uppercase, and in the second case is uppercase.

Let's create variables of some types as an example, and also consider how to create variables and assign them various values.


Rules when working with variables

  • First, variables of type float and double must have the first letter of the variable type name after the value without any spaces. This is a necessary requirement of the language itself to unambiguously understand what type the compiler should work with;
  • Second, in a variable of type char, the value is a single character enclosed in single quotation marks (for example, 'Z' or 'I');
  • Third, , there is also a new type named var. It's auto-type. To be even more precise, it's not exactly a type, it's just a "note to the compiler from the developer" that says "determine the type you want, please". And the compiler itself will choose the necessary type for your variable, the only nuance, in this variable it is necessary to immediately put some value. this approach is called implicit typing«;

The main rules when creating variables are:

  1. Correctly selected type;
  2. A friendly and unambiguous variable name;

The work with the rest of the standard types is exactly the same as with the examples above. Be smart about choosing the type and name of a variable.