在C#中,可以通過以下方法來取得一個數的絕對值:
int num = -5;
int absNum = Math.Abs(num);
Console.WriteLine(absNum); // 輸出:5
int num = -5;
int absNum = num < 0 ? -num : num;
Console.WriteLine(absNum); // 輸出:5
int num = -5;
int absNum = (num ^ (num >> 31)) - (num >> 31);
Console.WriteLine(absNum); // 輸出:5