chapter 3: Variables and Expressions - Beginning C# 7 Programming with Visual Studio 2017
Perhaps the most basic description of a computer program is that it is a series of operations that manipulate data.
third type of comment in C#
/// A special comment that start with three / symbols instead of two.
You can configure Visual Studio to extract the text after these comments and create a specially formatted text file when a project is compiled.you can then use it to create documentation.
XML Documentation Comments (C# Programming Guide)
#region and #endregion keywords
How to achieve the code outlining functionality. You can do this with the #region and #endregion keywords, which define the start and end of a region of code that can be expanded and collapsed.
#region Using directives using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; #endregion
Integer Types
TYPE | ALIAS FOR | ALLOWED VALUES |
---|---|---|
sbyte | System.SByte | Integer between −128 and 127 |
byte | System.Byte | Integer between 0 and 255 |
short | System.Int16 | Integer between −32768 and 32767 |
ushort | System.UInt16 | Integer between 0 and 65535 |
int | System.Int32 | Integer between −2147483648 and 2147483647 |
uint | System.UInt32 | Integer between 0 and 4294967295 |
long | System.Int64 | Integer between −9223372036854775808 and 9223372036854775807 |
ulong | System.UInt64 | Integer between 0 and 18446744073709551615 |
The names you use for these types in C# are aliases for the types defined in the framework.
Floating-Point Types
TYPE | ALIAS FOR | FORM | MIN M | MAX M | MIN E | MAX E | APPROX MIN VALUE | APPROX MAX VALUE |
---|---|---|---|---|---|---|---|---|
float | System.Single | 6m* | 0 | -149 | 104 | |||
double | System.Double | 6m* | 0 | -1075 | 970 | |||
decimal | System.Decimal | 6m* | 0 | -28 | 0 |
Text and Boolean Types
TYPE | ALIAS FOR | ALLOWED VALUES |
---|---|---|
char | System.Char | Single Vunicode character, stored as an integer between 0 and 65535 |
bool | System.Boolean | Boolean value, true or false |
string | System.String | A sequence of characters |
Note that there is no uper limit on the number of characters making up a string, because it can use varying amounts of memory.
Literal Values
TYPE(S) | CATEGORY | SUFFIX | EXAMPLE/ALLOWED VALUES |
---|---|---|---|
bool | Boolean | None | True or False |
int, uint, long, ulong | Integer | None | 100 |
uint, ulong | Integer | u or U | 100U |
long, ulong | Integer | l or L | 100L |
ulong | Integer | ul, uL, Ul, UL, lu, lU, Lu, or LU | 100UL |
float | Real | f or F | 1.5F |
double | Real | None, d, or D | 1.5 |
decimal | Real m or M | 1.5M | |
char | Character | None | ‘a’, or escape sequence |
string | String | None | “a. . . a”, may include escape sequences |
digit separators
public const double Pi = 3.141_592_653_589_793_238_462_643_383_279_502;
原義字串Verbatim strings
string location = "C:\\Temp\\MyDir\\MyFile.doc";
// location2 和 location相同
string location2 = @"C:\Temp\MyDir\MyFile.doc";
自增和自減運算子
int var1, var2 = 5, var3 = 6;
// var1 = 25;
var1 = var2++ * --var3;
Placing one of these operators before its operand means that the operand is affected before any other computation takes place. Placing it after the operand means that the operand is affected after all other computation of the expression is completed.
$""
double firstNumber = 5, secondNumber = 6;
// The sum of 5 and 6 is 11
Console.WriteLine($"The sum of {firstNumber} and {secondNumber} is " + $"{firstNumber + secondNumber}.");