官术网_书友最值得收藏!

Declaring variables

In the previous chapter, we saw how variables are written and touched on the high-level functionality that they provide. However, we're still missing the syntax that makes all of that possible. Variables don't just appear at the top of a C# script; they have to be declared according to certain rules and requirements. At its most basic level, a variable statement needs to satisfy the following requirements:

  • The type of data the variable will store needs to be specified.
  • The variable has to have a unique name.
  • If there is an assigned value, it must match the specified type.
  • The variable declaration needs to end with a semicolon.

The result of adhering to these rules is the following syntax:

dataType uniqueName = value;

Variables need unique names to avoid conflicts with words that have already been taken by C#, which are called keywords. You can find the full list of protected keywords at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/index.

This is simple, neat, and efficient. However, a programming language wouldn't be useful in the long run if there was only one way of creating something as pervasive as variables. Complex applications and games have different use cases and scenarios, all of which have unique C# syntax.

Type and value declarations

The most common scenario for creating variables is one that has all of the required information available when the declaration is made. For instance, if we knew a player's age, storing it would be as easy as doing the following:

int currentAge = 32;

Here, all of the basic requirements have been met:

  • A data type is specified, which is int (short for integer).
  • A unique name is used, which is currentAge.
  • 32 is an integer, which matches the specified data type.
  • The statement ends with a semicolon.

However, there will be scenarios where you'll want to declare a variable without knowing its value right away. We'll talk about this topic in the following section.

Type-only declarations

Consider another scenario: you know the type of data you want a variable to store, as well as its name, but not its value. The value will be computed and assigned somewhere else, but you still need to declare the variable at the top of the script.

This situation is perfect for a type-only declaration:

int currentAge;

Only the type (int) and unique name (currentAge) are defined, but the statement is still valid because we've followed the rules. With no assigned value, default values will be assigned according to the variable's type. In this case, currentAge will be set to 0, which matches the int type. Whenever the actual value is available, it can easily be set in a separate statement by referencing the variable name and assigning it a value:

currentAge = 32;

You can find a complete list of all C# types and their default values at https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/default-values.

At this point, you might be asking why, so far, our variables haven't included the public keyword, called an access modifier, which we saw in earlier scripting examples. The answer is that we didn't have the necessary foundation to talk about them with any clarity. Now that we have that foundation, it's time to revisit them in detail.

主站蜘蛛池模板: 广南县| 玉龙| 芦溪县| 兴安盟| 扬州市| 永定县| 佛山市| 湘乡市| 汝南县| 宝应县| 贵溪市| 郯城县| 东莞市| 苍溪县| 临澧县| 余江县| 辽中县| 博湖县| 陆丰市| 张家港市| 拉孜县| 武胜县| 长葛市| 竹北市| 博爱县| 灵武市| 郴州市| 察隅县| 车致| 华容县| 比如县| 太仓市| 德庆县| 东丽区| 南通市| 无为县| 随州市| 博白县| 年辖:市辖区| 涞源县| 汪清县|