Chapter 5 : More about Variables - Note for BEGINNING C#7 Programming with Visual Studio 2017.pdf
implicit conversion
The implicit conversion rule for these types is this: Any type A whose range of possible values completely fits inside the range of possible values of type B can be implicitly converted into that type.
explicit conversion
Attempting to fit a value into a variable when that value is too big for the type of that variable results in an overflow, and this is the situation you want to check for.
checked(<expression>)
unchecked(<expression>)
byte destinationVar;
short sourceVar = 281;
// defatult unchecked, throw System.OverflowException
destinationVar = checked((byte)sourceVar);
隱式轉換和顯式轉換(強制型別轉換)總結
- 根據型別的空間大小,空間小的型別轉換為空間大的型別為隱式轉換,反之則為顯示轉換。
- 編譯器自動進行隱式轉換,顯式轉換則需要新增(type)強制型別轉換符或者利用System.Convert中的型別轉換函式。
- 強制型別轉換有可能丟擲System.OverflowException異常System.FormatException異常。強制型別轉換有可能丟擲System.OverflowException異常System.FormatException異常。
- Note: 兩個short型別的整數相加返回的是int型別的整數
foreach Loops
A foreach loop enables you to address each element in an array using this simple syntax:
foreach (<baseType> <name> in <array>) { // can use <name> for each element }
string[] friendNames = { "Todd Anthony", "Kevin Holton", "Shane Laigle" };
foreach (string friendName in friendNames)
{
WriteLine(friendName);
}
Multidimensional Arrays
double[,] hillHeight = { { 1, 2, 3, 4}, { 2, 3, 4, 5}, { 3, 4, 5, 6} };
double[,] hillHeight2 = new double[3, 4];
// WriteLine(hillHeight[2, 1]);
foreach (double height in hillHeight)
{
WriteLine($"{height}");
}
ReadKey();
Arrays of Arrays(jagged arrays)
The syntax of declaring arrays of arrays.
- The syntax for declaring arrays of arrays.
//int[][] jaggedIntArray;
//jaggedIntArray = new int[2][];
//jaggedIntArray[0] = new int[3];
//jaggedIntArray[1] = new int[4];
//jaggedIntArray = new int[3][] { new int[] { 1, 2, 3 }, new int[] { 1 }, new int[] { 1, 2 } };
int[][] jaggedIntArray = { new int[] { 1, 2, 3 }, new int[] { 1 }, new int[] { 1, 2 } };
- You must loop through every sub-array, as well as through the array itself.
int[][] divisors1To10 = { new int[] { 1 },
new int[] { 1, 2 },
new int[] { 1, 3 },
new int[] { 1, 2, 4 },
new int[] { 1, 5 },
new int[] { 1, 2, 3, 6 },
new int[] { 1, 7 },
new int[] { 1, 2, 4, 8 },
new int[] { 1, 3, 9 },
new int[] { 1, 2, 5, 10 } };
foreach (int[] divisorsOfInt in divisors1To10)
{
foreach(int divisor in divisorsOfInt)
{
WriteLine(divisor);
}
}
Pattern Matching with switch case expression
- In C# 7 it is possible to match patterns in a switch case based on the type of variable.The value of that type when there is a case match, is placed into the declared variable.
class Program
{
static void Main(string[] args)
{
string[] friendNames = { "Todd Anthony", "Kevin Holton", "Shane Laigle", null, "" };
foreach (var friendName in friendNames)
{
switch (friendName)
{
// Once declared, t can be used to access the value stored in friendName and the methods and properties
// available from the string type.
case string t when t.StartsWith("T"):
WriteLine("This friends name starts with a 'T': " + $"{friendName} and is {t.Length - 1} letters long ");
break;
case string e when e.Length == 0:
WriteLine("There is a string in the array with no value");
break;
case null:
WriteLine("There is a string in the array with no value");
break;
// using the var declaration of x to capture any other variable type.
case var x:
WriteLine("This is the var pattern of type: " + $"{x.GetType().Name}");
break;
default:
break;
}
}
int sum = 0, total = 0, intValue = 0;
// The question mark lets the compiler know that this int[] array can contain null objects.
int?[] myIntArray = new int?[7] { 5, intValue, 9, 10, null, 2, 99 };
foreach (var integer in myIntArray)
{
switch (integer)
{
case 0:
WriteLine($"Integer number '{total}' has a default value of 0");
total++;
break;
case int value:
sum += value;
WriteLine($"Integer number '{total}' has a value of {value}");
total++;
break;
case null:
WriteLine($"Integer number '{ total }' is null");
total++;
break;
default:
break;
}
}
WriteLine($"The sum of all {total} integers is {sum}");
ReadKey();
}
}
String Manipulation
A string type variable can be treated as a read-only array of char variables.
METHOD | DESCRIPTION |
---|---|
<string>.Length | 返回字串的長度 |
<string>.ToCharArray() | 返回字元陣列 |
<string>.ToLower() | 返回小寫字串(不改變原有字串,下同) |
<string>.ToUpper() | 返回大寫字串 |
<string>.Trim() | 去除開頭和結尾的空格(還提供了去除其它字元的過載方法) |
<string>.TrimStart() | 去除開頭的空格 |
<string>.TrimEnd() | 去除結尾的空格 |
<string>.PadLeft() | 在左側填充空格 |
<string>.PadRight() | 在右側填充空格 |