Do predefined types have different values in .Net Core?

To answer this question, we will build one Console App .Net Core application in Visual Studio Code installed on Kali Linux distro. The program will introduce us to all types of data, their maximum and minimum values. We will also add a constant to the program and we will see what will get for the results. Then we will check the same code in Visual Studio .Net 2019 Community. But let’s start from beginning. What is programming? Basically, it is a skill for you to tell your computer to behave in a certain way with the code that your computer understands. In order to be involved in programming, it is necessary to have a computer, operating system, Internet connection, IDE – development environment and that you know the certain programming language. In our case, this is C# programming language which we are currently learning. Also, we have Visual Studio Code installed on the Linux operating system and Kali distro installed on an older laptop.

In your case, it may be different. For example, to build .Net Core applications on a different operating system, it’s important to understand that learning C# programming language does not require too many software, hardware and technical costs. Our advice to you is whenever you have an older or less configured computer; install a Linux operating system on it. And use it to learn. In order for your computer to understand your code, you are forwarding commands to your computer in the form of a single line of code or multiple grouped lines of code called statements that the computer understands. You group the statements into methods, methods into classes, and do it as simply, accurately, and without a single mistake. Every piece of code must be without any error, optimized and tested. Also, always try to write the code only once while the same code is used and called repeatedly in the project. But where do the types, variables, their values ​​and constants fit in?

The variables are words that describe a place in memory that contains values ​​that you can change. To understand this, it is best to think of a variable as a small box that stores some value in memory. So, there are various boxes. For example, there is an integer box or an integer type in which you can put an integer value. There is also a decimal box or a decimal type where you can put a decimal value. There is a boolean box that you can only put true or false value. Sometimes you will need to decide is it something true or false. The boolean type is awesome for it. But you can’t put an integer value in a boolean box. So, you put values ​​in specific boxes for that value that store your value in your computer’s RAM – Random Access Memory. This is how the program remembers the data processed.

To control memory usage and data processing speed, you should always consider how much space your variable will use and select the appropriate type for it. But what is a constant? The constant is a special box or special data type that must contain and type. This means that as you create the constant you also specify its type. Variables can change value, but constants cannot. You can add or subtract a number to the variable while the constant is always the same. It is used when you have some value like Pi in mathematics. Its value is 3.14159265359; but lest you constantly write this number whenever you need it in the program; simply create a constant of type Pi of decimal and assign it a value of 3.14159265359. So, when you need this value, you write Pi in your code, while the program knows that the Pi value is the same as if you wrote 3.14159265359. To see how this works, we move on to coding a program that contains predefined types used in the C# programming language.

Open Visual Studio Code and create a new project. Save the program as variable. If you don’t know how to do it, see the previous post here . Delete the following line of code.

Console.WriteLine("Hello World!");

With almost every program, we will first delete the line of previous code. Add the following line of code:

using static System.Console;

At the beginning of each program, we use using statements to instruct the compiler where to find the classes used in the program. Usage statements are not executed but they help us shorten the typing. However, if you want to use the using statement with static classes, then you must use the static directive using. There is a static class in our Console program, which also means that all of its members are static. We will learn about static classes, functions and fields as you learn the classes. Until then, all you need to know is that there are static classes, functions, and fields. Variables can also be declared static when it makes sense to do so. Enter the following line of code within the Main () function that will print the title of our program on the console:

WriteLine("\t\tDATA TYPES, VARIABLES AND CONSTANTS IN C#\n");

Because we use a using statement with the Console static class, we can now type shorter instead of:

System.Console.WriteLine("…");

or

Console.WriteLine("…");

or only

WriteLine("…");

Also, this applies to all methods in the Console class. In the first line of code in the Main () function, we tell the program to make a space of two tabs \ t \ t or 8 characters, print the program title on the console, and skip one line. If you are using the Windows operating system and the program is running in PowerShell, then one tab \ t will count 8 characters. You can also use the method to print text on the console:

Write("…");

The WriteLine () method prints text in one line. For example, if you use the WriteLine () method twice, that the method will print two texts, one below the other. However, if you use the Write () method, that method will print both texts in the same order. The only difference is that the WriteLine () method after your text automatically inserts an output sequence \ n behind your text at runtime, which of course you don’t see in the code. The output sequence \ n is the same as if you pressed the Enter key on a keyboard. You can use multiple identical or different output sequences in your text. See the following table of output sequences.

 

See the following line of code:

System.Console.WriteLine("\t\tDATA TYPES, VARIABLES AND CONSTANTS IN C#" + Environment.NewLine);

Instead of the output sequence \ n we used the property NewLine of the Environment class. Sometimes code developers use this property instead of the output sequence \ n for better the code readability. C# programming language has predefined data types that we often use. They are located in the System namespace. You can also create your own data type, and you will often do this when creating and using classes or structures. The predefined types in C# programming language are:

  • Value Types

           1. Logical type – bool

           2. Numerical types     

                a)  Whole numbers – byte, short, int, long

                b)  Unsigned numbers – sbyte, ushort, uint, ulong

                c)   Real numbers – float, double, decimal

            3. Character Type – char

  • References Types

          1. Character type – string

          2. Object type – object

There is another type dynamic that you won’t see in IntelliSense while typing code and that we don’t recommend using. The essential difference between value and reference types is in how they are processed in memory. Value data types store value in memory while reference data types are more complex. They contain a reference to that value in addition to the value. Reference types are most commonly used when working with strings, classes, delegates, etc. First, look at the logical data type. Write the following line of code in your program:

WriteLine("bool or System.Boolean ... {0}, {1}", true, false);

The logical operator can only be true or false. It is most commonly used when you want to question a value or whether it is true or false. If you want to display some value or value of some change on the screen, then you need to do so by placing curly braces {} and their indexes anywhere in the text between quotation marks. Values ​​or changes to text changes in quotation marks. It’s the older way of doing things. You can write the same line like this:

WriteLine($"bool or System.Boolean ... {true}, {false}");

If you want to add a non-minus number to variable and you are sure that number will never be greater than 255, then you will be selected byte type. Enter the following line of code:

WriteLine("byte or System.Byte ... {0}, {1}", 0, 255);

Byte type is often used in the byte array ( byte[] ) that contain an image. You will learn the arrays later. It is important that you understand that the byte type can be any whole number from 0 to 255. Any other number will cause an error. Enter the following line of code:

WriteLine("sbyte or System.SByte ... {0}, {1}", -128, 127);

Unlike byte, the sbyte data type is used for numbers with a minus prefix. However, its rank is smaller and therefore only accepts a number from -128 to 127. Enter the following line of code:

WriteLine("char or System.Char ... {0}, {1}", "A", 9);

The Value type char is a type which represents only one Unicode character and takes only 2 bytes. This type can only contain one character. It can be a letter, number or any character in a string of Unicode characters. The type char is often used in string searches, they may also contain some offset sequence or hexadecimal character. Skip one line into the code editor and enter the following lines of code:

WriteLine();

decimal decimalVariable = decimal.MinValue;

WriteLine("decimal or System.Decimal ... {0},\n\t\t\t {1}\n",

decimalVariable, decimalVariable = decimal.MaxValue);


The floating-point decimal type is mainly used for financial calculations, which requires accurate arithmetic and high precision in processing numbers with base 10. In the previous code, we first declared the variable decimal and assigned his smallest possible value that this type contains. As you can see, the variable declaration is declared as follows:

decimal money = 99.99M;

Put the data type first, then name the variable yourself as you like and then assign it a character without quotes when assigning numbers by using the character =. If you are wondering what M is worth, it is a numerical suffix. Some variables have their suffix to distinguish them from other types of data. For example, this is how you declared variable money:

decimal money = 99.99;

But without the suffix M in the previous line of code, you would get an error. Because according to the negotiator, the translator would think that you assign a double value to the variable decimal money. So, you have to use numerical suffixes. The numerical suffixes in the C# programming language are:

  • F – float
  • D – double
  • M – decimal
  • L – long
  • U – unsigned
  • UL – unsigned long

When you name a variable, there are limitations. First, your variable must not contain any special characters other than the underscore _. Then the name of your variable must not begin with the number 1money. Your variable cannot contain two or more words separately my money. You would write this like myMoney or MyMoney or my_money etc. Your variable cannot have the same name as one of the key words in the C# programming language. Check out the following picture:

In C# programming language, predefined types are structures and classes and as such have many methods. When you want to use methods that have a decimal type, then you write the type, write a dot and use one of the IntelliSense methods you need. In the previous code, we use the MinValue () method of the decimal data type to tell us what the lowest decimal value is and assign that value to the variable decimalVariable that we will use to show us the minimum value in the console. Enter the following line of code:

double doubleVariable = double.MinValue;

WriteLine("double or System.Double ... {0}, \n\t\t\t {1}\n",

doubleVariable, doubleVariable = double.MaxValue);

The type double as opposed to decimal is twice as large while using up to half the memory space. However, the double type is not guaranteed to be correct. Used it when precision is not important to you.

0.2 + 0.1 does Not equal 0.3

Enter the following lines of code:

float floatVariable = float.MinValue;

WriteLine("float or System.Single ... {0}, \n\t\t\t {1}\n",

floatVariable, floatVariable = float.MaxValue);

The float type is often used when you have some smaller floating-point numbers. For example, when creating a program that converts temperatures. When using this data type, be sure to use its numeric suffix F. Enter the following lines of code:

int intVariable = int.MinValue;

WriteLine("int or System.Int32 ... {0}, \n\t\t\t {1}\n",

intVariable, intVariable = int.MaxValue);

Integer type is the most commonly used data type for whole numbers. If you are sure that you will do some calculations in the program ranging from -2147483648 to 2147483647 then the integer type is a great choice. Enter the following lines of code:

uint uintVariable = uint.MaxValue;

WriteLine("uint or System.UInt32 ... {0}, \n\t\t\t {1}\n",

uintVariable, uintVariable = uint.MaxValue);

long longVariable = long.MinValue;

WriteLine("long or System.Int64 ... {0}, \n\t\t\t {1}\n",

longVariable, longVariable = long.MaxValue);

ulong ulongVariable = ulong.MinValue;

WriteLine("ulong or System.UInt64 ... {0}, \n\t\t\t {1}\n",

ulongVariable, ulongVariable = ulong.MaxValue);

short shortVariable = short.MinValue;

WriteLine("short or System.Int16 ... {0}, \n\t\t\t {1}\n",

shortVariable, shortVariable = short.MaxValue);

ushort ushortVariable = ushort.MinValue;

WriteLine("ushort or System.UInt16 ... {0}, \n\t\t\t {1}\n",

ushortVariable, ushortVariable = ushort.MaxValue);

All these number types are different only in the rank they store. The  types uint, ushort and ulong are types of numbers without minus suffix . Enter the following lines of code.

string stringVariable = "A string can have 2GB or over 1 billion charaters.";

WriteLine("string or System.String ... {0}\n", stringVariable);

The string type is also most commonly used in almost all programs. It is a reference type and it can contain over a billion characters. It is used for any kind of text while its memory usage depends on the number of characters you use in the string. In C# programming language, the data type string is not just a string of type char but a serious class that contains many useful methods that you will use when working with text. Enter the following lines of code.

object objectVariable = "An object can hold any type";

WriteLine("object or System.Object ... {0}\n", objectVariable);

The type object is a special type that stores any type of data. However, you should avoid this type of data whenever possible because of his poor performance. Enter the following lines of code:

const decimal PI = 3.14159265359M;

WriteLine("You cannot change constant value after initalization.\n Constant PI = {0} always.\n", PI);

// ReadLine();

// ReadKey();

And finally, let’s see how constants are declared. As we said before, use the const keyword, then write the variable and assign it a value. In the transition code, the PI value cannot be changed. You may be wondering if there are any other types of data in the C# programming language? The answer is yes, many; such as generic types and other classes such as the DateTime structure in charge of working with dates and time. Basically, this means that many of the things you want to program have already been programmed and can be used in your programs, but you can also create your own data types. Notice the last commented lines of code. These are methods that stop the flow of the program and expect the user to enter any kay on the keyboard. When you code in a Visual Studio .net development environment and do not use any of the previous commands, your program when start, it will open the console, execute the program, and then close the console so quickly that you will not see anything. If you code in Visual Studio Code, the terminal or powershell panel will not close and you will be able to see the results of your program. View full program code:

using System;

using static System.Console;

namespace variables

{

class Program

{

static void Main(string[] args)

{

System.Console.WriteLine("\t\tDATA TYPES, VARIABLES AND CONSTANTS IN C#\n");

WriteLine($"bool or System.Boolean ... {true}, {false}");

WriteLine("byte or System.Byte ... {0}, {1}", 0, 255);

WriteLine("sbyte or System.SByte ... {0}, {1}", -128, 127);

WriteLine("char or System.Char ... {0}, {1}", "A", 9);

WriteLine();

decimal decimalVariable = decimal.MinValue;

WriteLine("decimal or System.Decimal ... {0},\n\t\t\t {1}\n",

decimalVariable, decimalVariable = decimal.MaxValue);

double doubleVariable = double.MinValue;

WriteLine("double or System.Double ... {0}, \n\t\t\t {1}\n",

doubleVariable, doubleVariable = double.MaxValue);

float floatVariable = float.MinValue;

WriteLine("float or System.Single ... {0}, \n\t\t\t {1}\n",

floatVariable, floatVariable = float.MaxValue);

int intVariable = int.MinValue;

WriteLine("int or System.Int32 ... {0}, \n\t\t\t {1}\n",

intVariable, intVariable = int.MaxValue);

uint uintVariable = uint.MaxValue;

WriteLine("uint or System.UInt32 ... {0}, \n\t\t\t {1}\n",

uintVariable, uintVariable = uint.MaxValue);

long longVariable = long.MinValue;

WriteLine("long or System.Int64 ... {0}, \n\t\t\t {1}\n",

longVariable, longVariable = long.MaxValue);

ulong ulongVariable = ulong.MinValue;

WriteLine("ulong or System.UInt64 ... {0}, \n\t\t\t {1}\n",

ulongVariable, ulongVariable = ulong.MaxValue);

short shortVariable = short.MinValue;

WriteLine("short or System.Int16 ... {0}, \n\t\t\t {1}\n",

shortVariable, shortVariable = short.MaxValue);

ushort ushortVariable = ushort.MinValue;

WriteLine("ushort or System.UInt16 ... {0}, \n\t\t\t {1}\n",

ushortVariable, ushortVariable = ushort.MaxValue);

string stringVariable = "A string can have 2GB or over 1 billion charaters.";

WriteLine("string or System.String ... {0}\n", stringVariable);

object objectVariable = "An object can hold any type";

WriteLine("object or System.Object ... {0}\n", objectVariable);

const decimal PI = 3.14159265359M;

WriteLine("You cannot change constant value after initalization.\n Constant PI = {0} always.\n", PI);

// ReadLine();

}

}

}

When we run our program with the dotnet run command in the terminal panel, you will get the following result:

            DATA TYPES, VARIABLES AND CONSTANTS IN C#

bool or System.Boolean … True, False

byte or System.Byte … 0, 255

sbyte or System.SByte … -128, 127

char or System.Char … A, 9

 

decimal or System.Decimal … -79228162514264337593543950335, 79228162514264337593543950335

double or System.Double … -1.79769313486232E+308, 1.79769313486232E+308

float or System.Single … -3.402823E+38, 3.402823E+38

 

int or System.Int32 … -2147483648, 2147483647

uint or System.UInt32 … 4294967295, 4294967295

 

long or System.Int64 … -9223372036854775808, 9223372036854775807

ulong or System.UInt64 … 0, 18446744073709551615

 

short or System.Int16 … -32768, 32767

ushort or System.UInt16 … 0, 65535

 

string or System.String … A string can have 2GB or over 1 billion charaters.

object or System.Object … An object can hold any type

You cannot change constant value after initialization.

Constant PI = 3.14159265359 always.

In the results, you can see the smallest and highest values ​​of all predefined types. You can also see how this program looks in video:

When writing a statement or a single line of code, the space is not that important because the statement ends with a semicolon ; In the video, you can see that when you break a statement into another line of code, sometimes an error may occur. Why? Because we broke the statement where the text is in quotation marks. You have to be careful where you break your statement. And for the end of this post, we want to answer our question. Do predefined types have different values ​​in .Net Core? We will copy this program, move it to Visual Studio .net to run and you can see the answer when you compare our values ​​with the values ​​in next the image. They are the same!

 

 

Leave a Reply

Close Menu