C# Math, String, Array, Datetime 類別
阿新 • • 發佈:2018-12-23
在這裡記錄一些常用的 C# 內建的靜態類別
Math 方法
C# Math 類別完整的方法,可以參考官網C# Math類別
這裡列出一些常見的方法
符號 | 說明 |
---|---|
Math.PI | 圓周 π |
Math.Max(n1, n2) | 比較兩值,返回最大值 |
Math.Min(n1, n2) | 較兩值,返回最小值 |
Math.Abs(n) | 取得絕對值 |
Math.Round(n) | 四捨五入 |
Math.Floor(n) | 無條件捨去 |
Math.Ceiling(n) | 無條件進位 |
Math.Sqrt(n) | 開平方根 |
Array 方法
C# Array 類別完整的方法,可以參考 官網C# Array類別
符號 | 說明 |
---|---|
Array.Reverse(ary) | 反轉整個一維 Array 中的專案順序 |
Array.Sort(ary) | 對陣列內容排序,可接受數值、字串排序 |
String 方法
符號 | 說明 |
---|---|
String.Concat(s1, s2) | 結合字串 |
String.Equals(s1, s2) | 比對字串,返回布林值 |
DateTime 方法
C# DateTime 類別完整的方法,可以參考官網C# DateTime 類別
符號 | 說明 |
---|---|
DateTime.Now | 取得目前的日期及時間值 |
DateTime.Today | 取得今天日期值 |
DateTime取得時間後,通常還需要經過轉換才能使用
範例
取得某年某月的天數
C#取得某年某月的天數,範例如下:
比較兩個時間
範例 (參考自microsoft官網說明手冊)
如果你喜歡我們的文章內容,請在這裡按個讚
Tweet