TOC

This article is currently in the process of being translated into Persian (~20% done).

The Basics:

Variables

یک متغیر می تواند با یک اتاق ذخیره مقایسه شود و این برای برنامه نویس ضروری است. در C# یک متغیر به شکل زیر اعلام شده است:

<data type> <name>;

An example could look like this:

string name;

این ابتدایی ترین نسخه است ، اما متغیر تا الان مقدار ندارد. می توانید بعدا به آن مقدار دهید یا همزمان با اعلام متغیر به ان مقدار دهید مانند این:

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

If this variable is not local to the method you're currently working in (e.g. a class member variable), you might want to assign a visibility to the variable:

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

و یک مثال کاملتر:

private string name = "John Doe";

The visibility part is related to classes, so you can find a more complete explanation of it in the chapter about classes. Let's concentrate on the variable part with an example where we actually use a couple of them:

using System;

namespace ConsoleApplication1
{
    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();
}
    }
}

خوب، تا اینجا خیلی توضیح داده شد، بنابراین می توانیم به بخش جالب بریم. نخست، ما یک جفت متغیر از نوع string اعلام کردیم. یک string بسادگی شامل متن است همانطوری که می بینید، ما به آنها مستقیم مقدار دادیم. مرحله بعدی، ما یک خط متن را به خروجی کنسول می فرستیم، جایی که ما از دو تا متغیر استفاده می کنیم. رشته با استفاده از کارکتر + برای جمع کردن بخش های مختلف استفاده شده است.

Next, 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 enter it into the firstName variable. Once the user presses the Enter key, the new first name is assigned to the variable, and in the next line we output the name presentation again, to show the change. We have just used our first variable and the single most important feature of a variable: The ability to change its value at runtime.

Another interesting example is doing math. Here is one, based on a lot of the same code we have just used:

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 this in our Main method, and try it out. The only new "trick" we use here, is the int.Parse() method. It simply reads a string and converts it into an integer. As you can see, this application makes no effort to validate the user input, and if you enter something which is not a number, an exception will be raised. More about those later.

Variables & scope

So far, we have only used local variables, which are variables defined and used within the same method. In C#, a variable defined inside a method can't be used outside of this method - that's why it's called local. If you're familiar with other programming languages, you may also know about global variables, which can be accessed from more places, but C# doesn't support the concept of global variables. Instead, you can define a field on a class, which can be accessed from all the methods of this class. Allow me to demonstrate this with an example:

using System;

namespace VariableScope
{
    class Program
    {
private static string helloClass = "Hello, class!";

static void Main(string[] args)
{
    string helloLocal = "Hello, local!";
    Console.WriteLine(helloLocal);
    Console.WriteLine(Program.helloClass);
    DoStuff();
}

static void DoStuff()
{
    Console.WriteLine("A message from DoStuff: " + Program.helloClass);
}
    }
}

Notice the helloClass member, declared on the class scope instead of inside a method - this will allow us to access it from both our Main() method as well as our own DoStuff() method. That is not true for our helloLocal variable, which has been declared inside the Main() method and can therefore only be used inside of this specific method.

The concept of differentiating between where a variable has been declared is called scoping and it prevents your code from becoming a huge mess of variables which can be changed from too many places. Another technique that helps us with this is called member visibility (in this case illustrated with the private keyword), which we'll discuss in the chapter about classes.

Summary

Variables allow you to store data of various types, e.g. text strings, numbers or custom objects. There are local variables, which are accessible inside of the method in which it has been defined, and then there are class fields, which can be accessed from all the methods of the class and even outside of the class, if the visibility permits it.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!