This article is currently in the process of being translated into Chinese (~7% done).
If you are fluent in Chinese, then please help us - just point to any untranslated element inside the article and click the translation button to get started. Or have a look at the current translation status for the Chinese language.
If you see a translation that you think looks wrong, then please consult the original article to make sure and then use the vote button to let us know about it.
Please help us by translating the following metadata for the article/chapter, if they are not already translated.
If you are not satisfied with the translation of a specific metadata item, you may vote it down - when it reaches a certain negative threshold, it will be removed. Please only submit an altered translation of a metadata item if you have good reasons to do so!
<data type> <name> = <value>;
<可见性> <变量类型> <变量名> = <值>;
private string name = "John Doe";
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);
这个例子的大部分已经解释过了，我们直接看有意思的部分。首先，我们定义了几个string类型的变量，string类型包含文本，如你所见，我们用文本直接对其赋值。接下来，我们向控制台输出了一行文本，这其中用到了两个变量。这个字符串是由 + 号连接的不同部分组成的。
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));
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:
private static string helloClass = "Hello, class!";
static void Main(string args)
string helloLocal = "Hello, local!";
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.
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.