在字串前加 @ 可避免跳脫序列,路徑字串更易讀:
// 一般寫法
string path = "\\\\FileShare\\Directory\\file.txt";
// 逐字字串
string escapedPath = @"\\FileShare\Directory\file.txt";
C# 與 Java 相似,支援隱式(擴大轉換)與顯式(縮小轉換)轉型。
| 來源型別 | 目標型別 |
|---|---|
| byte | short, ushort, int, uint, long, ulong, float, double, decimal |
| sbyte | short, int, long, float, double, decimal |
| short | int, long, float, double, decimal |
| ushort | int, uint, long, ulong, float, double, decimal |
| int | long, float, double, decimal |
| uint | long, ulong, float, double, decimal |
| long | float, double, decimal |
| ulong | float, double, decimal |
| float | double |
| char | ushort, int, uint, long, ulong, float, double, decimal |
long longVariable = 5483;
int intVariable = (int)longVariable; // 可能遺失資料
| 來源型別 | 目標型別 |
|---|---|
| byte | sbyte, char |
| sbyte | byte, ushort, uint, ulong, char |
| short | sbyte, byte, ushort, uint, ulong, char |
| ushort | sbyte, byte, short, char |
| int | sbyte, byte, short, ushort, uint, ulong, char |
| uint | sbyte, byte, short, ushort, int, char |
| long | sbyte, byte, short, ushort, int, uint, ulong, char |
| ulong | sbyte, byte, short, ushort, int, uint, long, char |
| float | sbyte, byte, short, ushort, int, uint, long, ulong, char, decimal |
| double | sbyte, byte, short, ushort, int, uint, long, ulong, char, float, decimal |
| char | sbyte, byte, short |
| decimal | sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double |