While working in programming, we have to utilize many types of variables and functions in the code to make it understandable for users and convenient to search. The main purpose of utilizing the variables and functions in any code is to avoid complexity and ambiguity and make it more convenient to use. Within C#, we can declare variables implicitly and explicitly. The implicit declaration doesn’t need to declare the type of a variable along with its name. In contrast, the explicit declaration requires you to include the type of a variable.
The implicit declaration needs the keyword “var” to declare any variable, and the compiler itself will interpret and determine the type of a variable required at some point. While in the explicit declaration, you will declare the type, and the compiler will only validate it. Within this article, we will be focusing on using the keyword “var” to implicitly declare variables in C#. Make sure to update your Linux system. The update process will help you ensure your system up to date with latest features and error-free because of these updates.
Example # 01:
We will be making a fresh start to our first example by creating a C# file for program code. You need to execute the Linux “touch” instruction on your console shell application. This file will be generated in your current working directory, most probably “home”.
We will be starting our first illustration to demonstrate the simple syntax of declaring and initializing any variable in C#. Start your C# code with the general “System” namespace usage in the code. This namespace is a must in every C# code for the use of general-purpose built-in functions. After this, we have created a simple and random class named “Test” and started a main() driver method in it.
Our main() method has been initializing an integer variable “x” with the value 12. The “int” keyword shows the type “integer” of a variable “x”. The very next line uses the WriteLine() function from the namespace system and its class “Console” to display the value of variable “x” on the shell.
Using the mcs compiler for C, run the file var.cs compiled or code very efficiently. The var.exe file has been created, and we have been utilizing the “mono-runtime” to run this exe file. The following output shows the simple display of variable “x” with its value:
The previous illustration shows the defining and initializing of any type of variable in C#. On the contrary, C# allows you to avoid using the data type with the variable and instead use the keyword “var”. The use of the “var” keyword makes the compiler interpret the type of a variable itself, and we don’t need to inform the compiler. So, we have updated our code below. Using the keyword “var”, we have initialized two variables, “x” and “y”, with positive integer and negative integer values, respectively. Then, WriteLine() function was used to display the values of both variables “x” and “y” on the console shell. Save and quit your text editor.
We have compiled this code with the “mcs” compiler and created its exe file. This var.exe file has been used for execution using mono-runtime. Both the variables are displayed, and we haven’t got any errors. This example shows how the keyword “var” is valuable in declaring variables:
After defining the variables implicitly, we will be looking at the type of variable using the “GetType” function of C#. For this, you need to implement the “Text” class of the “System” namespace in your C# code after the use of the “System” namespace separately. Within the “Test” class, the same main() method has been used. We used the same variables, “x” and “y”. Within the “WriteLine()” function of the Console class, we have been using the “GetType” called by the variable names to display the type of variables along with their values. We have used the curly brackets format in the string to display the values.
After compiling and running this program, we have to know the type of variables, “x” and “y”, separately. Both variables are of the same type, i.e., “Int32”, as per the C# compiler understanding.
Example # 02:
In the previous example, we have seen how the “var” variable and GetType() function can display the variable of integer type and its type on the console shell, respectively. Now, we will take a look at some other types of variables using the “var” keyword. So, we have initialized three variables, “x”, “y”, and “z”, with the “var” keyword within the main() driver code of this program.
Use the “System.Test” namespace line at the top of this code. These three variables contain different types of values in them, such as float, character, and string. We have been using the same Console.WriteLine() function to display each variables along with its type using the “GetType()” function with variable names. As all the variables are of different types, we expect the unique output on our screen.
Let’s save and compile our code with the “mcs” compiler. The compilation was quite successful, and no errors have been found. The exe file for this program code has been utilized within the mono instruction to execute the compiled code. The output displayed three variable values, “x”, “y”, and “z”, along with their types. It shows that “x” has type “double”, “y” has character type, and “z” has the string type.
Example # 03:
We have taken a look at most of the types found in C# to demonstrate the use of the “var” keyword for different and unique variables. Let’s take a look at the Boolean type variable value now. So, we have updated the variables in the main() function and replaced x, y, and z with b1, b2, and b3. We have been implicitly declaring them with the keyword “var” without stating their type.
The first two variables contain Boolean values “true” and “false”, and the last variable is empty. The same WriteLine() function is utilized here to display the variable value along with their types using the “GetType” function in this line.
After using this code in compilation and executing it with the C# runtime “mono” executer, we have the shown-blow result. It displays the first two variables are of “Boolean” type while the third one is of string type. This shows that any value that contains special characters, spaces, or empty will be considered a string.
In our article’s introductory paragraph, we have briefly discussed the idea of an implicit and explicit declaration of variables and made a comparison between them. We have expounded on the use of the “var” keyword with the help of the C# “GetType” function from the “Text” class of namespace “System” that has been stating the type of variables on the output. We have tried these examples for positive and negative integer, character, float, string, Boolean, and empty type of variables separately. The illustrations of the “var” keyword show that there will be no issue performing any operation on variables with the “var” keyword declaration. We hope you found this article helpful. Check the other Linux Hint articles for more tips and tutorials.