- Learning C# by Developing Games with Unity 2020
- Harrison Ferrone
- 566字
- 2021-06-11 18:44:50
Introducing operators
Operator symbols in programming languages represent the arithmetic, assignment, relational, and logical functionality that types can perform. Arithmetic operators represent basic math functions, while assignment operators perform math and assignment functions together on a given value. Relational and logical operators evaluate conditions between multiple values, such as greater than, less than, and equal to.
C# also offers bitwise and miscellaneous operators, but these won't come into play for you until you're well on your way to creating more complex applications.
At this point, it only makes sense to cover arithmetic and assignment operators, but we'll get to relational and logical functionality when it becomes relevant in the next chapter.
Arithmetic and assignments
You're already familiar with the arithmetic operator symbols from school:
- + for addition
- - for subtraction
- / for pision
- * for multiplication
C# operators follow the conventional order of operations, that is, evaluating parentheses first, then exponents, then multiplication, then pision, then addition, and finally subtraction (BEDMAS). For instance, the following equations will provide different results, even though they contain the same values and operators:
5 + 4 - 3 / 2 * 1 = 8
5 + (4 - 3) / 2 * 1 = 5
Operators work the same when applied to variables, as they do with literal values.
Assignment operators can be used as a shorthand replacement for any math operation by using any arithmetic and equals symbol together. For example, if we wanted to multiply a variable, both of the following options would produce the same result:
int currentAge = 32;
currentAge = currentAge * 2;
The second, alternative, way to do this is shown here:
int currentAge = 32;
currentAge *= 2;
The equals symbol is also considered an assignment operator in C#. The other assignment symbols follow the same syntax pattern as our preceding multiplication example: +=, -=, and /= for add and assign, subtract and assign, and pide and assign, respectively.
Strings are a special case when it comes to operators, as they can use the addition symbol to create patchwork text, as follows:
string fullName = "Joe" + "Smith";
This approach tends to produce clunky code, making string interpolation the preferred method for putting together different bits of text in most cases.
With this, we've learned that types have rules that govern what kind of operations and interactions they can have. However, we haven't seen that in practice, so we'll give it a shot in the next section.
Time for action – executing incorrect type operations
Let's do a little experiment: we'll try to multiply our string and float variables together, as we did earlier with our numbers:

If you look in the Console window, you'll see that we've got an error message letting us know that a string and a float can't be added. Whenever you see this type of error, go back and inspect your variable types for incompatibilities:

It's important that we clean up this example, as the compiler won't allow us to run our game at this point. Choose between a pair of backslashes (//) at the beginning of Debug.Log() on line 21, or delete it altogether.
That's as far as we need to go in terms of variables and types for the moment. Be sure to test yourself on this chapter's quiz before moving on!
- 深度實踐OpenStack:基于Python的OpenStack組件開發
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- Visual C++串口通信開發入門與編程實踐
- Linux核心技術從小白到大牛
- Silverlight魔幻銀燈
- Python算法詳解
- Web程序設計:ASP.NET(第2版)
- Julia數據科學應用
- Python機器學習開發實戰
- 寫給青少年的人工智能(Python版·微課視頻版)
- Android 游戲開發大全(第二版)
- Sitecore Cookbook for Developers
- 例說FPGA:可直接用于工程項目的第一手經驗
- Learning Azure DocumentDB
- Visual C++ 開發從入門到精通