首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 字串處理與型別轉換:逐字字串、隱式/顯式轉換完整對照表

     編輯:關於C語言

逐字字串:@ 符號用法

在字串前加 @ 可避免跳脫序列,路徑字串更易讀:

// 一般寫法
string path = "\\\\FileShare\\Directory\\file.txt";

// 逐字字串
string escapedPath = @"\\FileShare\Directory\file.txt";

型別轉換規則

C# 與 Java 相似,支援隱式(擴大轉換)與顯式(縮小轉換)轉型。

隱式轉換對照表

來源型別目標型別
byteshort, ushort, int, uint, long, ulong, float, double, decimal
sbyteshort, int, long, float, double, decimal
shortint, long, float, double, decimal
ushortint, uint, long, ulong, float, double, decimal
intlong, float, double, decimal
uintlong, ulong, float, double, decimal
longfloat, double, decimal
ulongfloat, double, decimal
floatdouble
charushort, int, uint, long, ulong, float, double, decimal

顯式轉換(強制轉型)

long longVariable = 5483;
int intVariable = (int)longVariable; // 可能遺失資料
來源型別目標型別
bytesbyte, char
sbytebyte, ushort, uint, ulong, char
shortsbyte, byte, ushort, uint, ulong, char
ushortsbyte, byte, short, char
intsbyte, byte, short, ushort, uint, ulong, char
uintsbyte, byte, short, ushort, int, char
longsbyte, byte, short, ushort, int, uint, ulong, char
ulongsbyte, byte, short, ushort, int, uint, long, char
floatsbyte, byte, short, ushort, int, uint, long, ulong, char, decimal
doublesbyte, byte, short, ushort, int, uint, long, ulong, char, float, decimal
charsbyte, byte, short
decimalsbyte, byte, short, ushort, int, uint, long, ulong, char, float, double
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved