Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Long.parse ใน C#

ใช้ long.Parse(string) แปลงสตริงเป็นจำนวนเต็ม 64 บิต (long/Int64) โยน Exception หากแปลงไม่ได้

ตัวอย่างโค้ด

using System;

class Demo
{
    static void Main()
    {
        string str = "7864646475767";
        long res = long.Parse(str);
        Console.WriteLine(res);
    }
}

ผลลัพธ์

7864646475767

ตัวเลือกที่ปลอดภัย: long.TryParse() คืนค่า bool ไม่โยน Exception