ตัวแปรแต่ละตัวใน C# มีชนิดเฉพาะ ซึ่งกำหนดขนาดและโครงสร้างหน่วยความจำ ช่วงค่าที่เก็บได้ และการดำเนินการที่ใช้ได้
ไวยากรณ์การประกาศ:
<data_type> <variable_list>;
ตัวอย่าง
int a, b; // ตัวแปร int 2 ตัว float f; // ตัวแปร float double d; // ตัวแปร double
ตัวอย่างการแสดงค่าตัวแปร
using System;
using System.Collections;
class Demo {
static void Main() {
int x = 50;
Console.WriteLine("Integer variable: " + x);
}
}
ผลลัพธ์
Integer variable: 50