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

Understanding variable scope

We're getting to the end of our pe into variables, but there's still one more important topic we need to cover: scope. Similar to access modifiers, which determine which outside classes can grab a variable's information, the variable scope is the term used to describe where a given variable exists and its access point within its containing class. 

There are three main levels of variable scope in C#:

  • Global scope refers to a variable that can be accessed by an entire program; in this case, a game. C# doesn't directly support global variables, but the concept is useful in certain cases, which we'll cover in Chapter 10, Revisiting Types, Methods, and Classes.
  • Class or member scope refers to a variable that is accessible anywhere in its containing class.
  • Local scope refers to a variable that is only accessible inside the specific block of code it's created in.

Take a look at the following screenshot. You don't need to put this into LearningCurve if you don't want to; it's only for visualization purposes at this point:

When we talk about code blocks, we're referring to the area inside any set of curly brackets. These brackets serve as a kind of visual hierarchy in programming; the farther right-indented they are, the deeper they are nested in the class.

Let's break down the class and local scope variables in the preceding screenshot:

  • characterClass is declared at the very top of the class, which means we can reference it by name anywhere inside LearningCurve. You might hear this concept referred to as variable visibility, which is a good way of thinking about it.
  • characterHealth is declared inside the Start() method, which means it is only visible inside that block of code. We can still access characterClass from Start() with no issue, but if we attempted to access characterHealth from anywhere but Start(), we would get an error. 
  • characterName is in the same boat as characterHealth; it can only be accessed from the CreateCharacter() method. This was just to illustrate that there can be multiple, even nested, local scopes in a single class.

If you spend enough time around programmers, you'll hear discussions (or arguments, depending on the time of day) about the best place to declare a variable. The answer is simpler than you might think: variables should be declared with their use in mind. If you have a variable that needs to be accessed throughout a class, make it a class variable. If you only need a variable in a specific section of code, declare it as a local variable.

Note that only class variables can be viewed in the Inspector window, which isn't an option for local or global variables.

With naming and scope in our toolbox, let's transport ourselves back to middle school math class and relearn how arithmetic operations work all over again!

主站蜘蛛池模板: 呼伦贝尔市| 呼玛县| 扎赉特旗| 淄博市| 伊宁市| 綦江县| 文山县| 宜宾县| 康保县| 克什克腾旗| 旌德县| 信丰县| 乌兰察布市| 韶山市| 泸州市| 汤阴县| 卓资县| 天气| 方城县| 北安市| 辽宁省| 花垣县| 枣庄市| 澎湖县| 上林县| 敖汉旗| 自治县| 阿巴嘎旗| 年辖:市辖区| 大庆市| 阿坝县| 湖口县| 松溪县| 乐东| 安福县| 岳普湖县| 夏津县| 当雄县| 洮南市| 迭部县| 迁安市|