A variable can be compared to the storage room, and the programmer requires that in C #, a variable is declared as follows:
<Data type> <name>;
An example might look like this:
String name;
This is the most basic version, generally, you want to assign visibility to the variable, and possibly assign a value at the same time. it's possible:
<visibility> <data type> <name> = <value>;
And with an example:
private string name = "Sandy Doe";
Part of visibility has been explained elsewhere in this tutorial, so let's see the focus on the variable part. We will jump straight into some of them:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string firstName = "Sandy";
string lastName = "Doe";
Console.WriteLine("Name: " + firstName + " " + lastName);
Console.WriteLine("Please enter a new first name:");
firstName = Console.ReadLine();
Console.WriteLine("New name: " + firstName + " " + lastName);
Console.ReadLine();
}
}
}
Okay, it has been explained long ago, so we will jump straight into the interesting part. First of all, we declare a string of string types. There is only text in a string, as you can see, because we give them direct value, we make a line of text in the console, where we use two variables, I use the "different parts" By using "to" letters
After this, we ask the user to enter a new first name, and then we use the readline () method to read the user input from the console and the first name variable. After the user presses the press key, the new first name variable is assigned, and in the next line we change the name to show the change. We have used the most important feature of our variables and variables: the ability to change its value at runtime
Another interesting example is math, here's a lot of code
int number1, number2;
Console.WriteLine("Please enter a number:");
number1 = int.Parse(Console.ReadLine());
Console.WriteLine("Thank you. One more:");
number2 = int.Parse(Console.ReadLine());
Console.WriteLine("Adding the two numbers: " + (number1 + number2));
Console.ReadLine();
Keep this in our main method and try it out, only a new "trick" has been used, it is the int.Parse () method. It only reads a string and converts it into an integer as you can see, this application does not attempt to validate user input, and if you enter something that is not a number, the exception will be Will grow. More about them later