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

คำต่อท้ายตัวเลข Literal ใน C#

C# ใช้ Numeric Suffixes ระบุชนิดข้อมูลของ Literal ตัวเลข โดยตรง

Suffixes พบบ่อย

  • L / l - long (Int64)
  • D / d - double
  • F / f - float (Single)
  • M / m - decimal
  • U / u - uint (UInt32)
  • UL / ul - ulong (UInt64)

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

using System;

public class Program
{
    public static void Main()
    {
        long val1 = 29345L;
        decimal val5 = 3245.5678M;
        double val2 = 297.325D;
        float val3 = 250.35F;
        uint val4 = 3456U;
        
        Console.WriteLine(val1);
        Console.WriteLine(val5);
        Console.WriteLine(val2);
        Console.WriteLine(val3);
        Console.WriteLine(val4);
    }
}

ผลลัพธ์

29345
3245.5678
297.325
250.35
3456

แนะนำ: ใช้ตัวพิมพ์ใหญ่ (L, D, F, M, U) เพื่อความชัดเจน (หลีกเลี่ยง l 混淆กับ 1)