Lesson 6: Learning to Define and Value Variables in C #

This is our lesson no 6 and today i will teach you about Learning to Define and Value Variables in C #. Learn how to define and value Variables in C #: A variable is a part of system memory that acts as a room for storing certain information goes.

Variables are the basis of the work of C # programs and can be defined as follows: For example, in the following code we have defined a variable of type String called name:

String name;

In the above code, its name. This is the simplest way to define a variable. But you may want a field of view or Visibility Specify a specific variable for your variable and methodize it when defining. This should work as follows be done :

<visibility> <data type> <name> = <value>;

In the example above <visibility <determines the field of view of the variable. That is to say what functions, classes or Other program-level variables can see, call, and manipulate this variable. <value <also that Specifies the initial value of the variable. The following code shows an example to fully define a variable in C #:

Private String name = “Tahlil adeh”;

In the following example code, we show how to define, value, and work with multiple variables in C #:

using System;
namespace ConsoleApplication1
{
12
 class Program
 {
 static void Main(string[] args)
 {
 string firstName = "John";
 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();
 }
 }
}
بسیار خب، بخش زیادی از کد مثال فوق را قبال توضیح داده ایم،

Well, we have already explained a large part of the above example code, so go straight to the section at this point We go where we want.


First of all, we have defined several variables of type Type String. A String can contain text
And as you can see in the example code, we have immediately set each String variable. Then a line of text 18 Is represented by two variables on the Consale output. From the character (+) to paste the text of two variables String and display them as a sentence is used.

Next, we ask the user to enter a new value for the firstName variable. For this purpose of
The ReadLine () command is used to receive an input value from the user and enter it into the firstName variable puts.

After the user enters the desired name, a new value is entered in the firstName variable and
Then again with the WriteLine.Consale () command, we show the sentence with a new name to the user.


In the above code, we have used only one variable, and this code is the most important feature of a variable, namely change in Shows Time Run. The next example to show the work of variables is to perform mathematical operations. The following code shows how to do this:

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();

Put the above code in the Main function of the previous example and run the program again. The only trick used in the second example, Use the Parse.int () method. This method reads a String text value and converts it to a numeric variable converts integer.


As you can see, in the second example code of the program there is no attempt to validate the input value The user did not do so and if the user enters a string value (text) and presses the Enter key, the program Will face, because it can not do math on the text. To solve this problem in later lessons
We will explain more.

%d bloggers like this: