Types of Variables
ดนุเดช นิลคูหา 650710079
What are Types of Variables?
คือ การตั้งชื่อให้กับตำแหน่งของหน่วยความจำ ทั้งนี้มีกฎของการตั้งชื่ออยู่โดยไม่สามารถตั้งชื่อโดยเป็น keyword หรือนำหน้าด้วยตัวเลข ในภาษา C# จะต้องทำการประกาศตัวแปรทั้งหมดก่อนจึงจะ สามารถใช้งานได้ และค่าที่เก็บไว้ในตัวแปรดังกล่าวสามารถเปลี่ยนแปลงได้ระหว่างการทำงานของโปรแกรม
Topic in Types of Variables?
Declaring Variables
Local Variables
lnstance Variables
Static Variables
Constant Variables
Readonly Variables
Declaring Variables
การเริ่มต้นการระบุประเภทข้อมูลของตัวแปรโดยมีรูปแบบ คือ ประเภทตัวแปร ตามด้วยชื่อตัวแปร
Syntax : type variableName = value;
Code
using System;
class Program {
static void Main() {
int integerVariable = 79; //ตัวแปรแบบจำนวนเต็ม integer
float floatVariable = 1.23f; //ตัวแปรทศนิยมแบบ float
double doubleVariable = 77.77; //ตัวแปรทศนิยมแบบ double
char charVariable = 'O'; //ตัวแปรประเภท char
string stringVariable = "Danudeth"; //ตัวแปรประเภท String
bool boolVariable = true; //ตัวแปรประเภท boolean
//แสดงค่าของตัวแปร
Console.WriteLine("Integer: " + integerVariable);
Console.WriteLine("Float: " + floatVariable);
Console.WriteLine("Double: " + doubleVariable);
Console.WriteLine("Char: " + charVariable);
Console.WriteLine("String: " + stringVariable);
Console.WriteLine("Boolean: " + boolVariable);
}
}
ทั้งนี้ในการประกาศตัวแปรในทุก ๆ ประเภทต้องคำนึงถึงกฎในการตั้งชื่อตัวแปรด้วย

Local Variables
ตัวแปรที่กำหนดไว้ใน Method เรียกว่า Local Variables มักใช้สำหรับจัดเก็บข้อมูลชั่วคราวภายในโปรแกรม โดยมีขอบเขตการจัดการเฉพาะใน Method นี้เท่านั้น และมีช่วงอายุของตัวแปร คือ เมื่อมีการเรียกใช้ Method ตัวแปรเหล่านี้จะถูกสร้าง และถูกทำลายหลังจาก Method นี้จบการทำงาน
using System;
class Calculator {
//เมธอด (Method)
public void SumInteger(int a, int b) {
int result = a + b; //Local Variables เพื่อใช้ตัวแปร result ชั่วคราวภายในโปรแกรม
Console.WriteLine("result : " + result);
}
public static void Main(String[] args) {
Calculator ip = new Calculator(); //สร้าง object เพื่อเรียกใช้ Method Sum
ip.SumInteger(100, 79); //เรียกใช้ Method Sum
}
}
Instance Variables
เป็นตัวแปรที่ถูกประกาศในคลาสแต่อยู่นอก Method ตัวแปรเหล่านี้จะถูกสร้างเมื่อมีการสร้าง object ของคลาสสำหรับใช้ในการเข้าถึง ตัวแปรต่าง ๆ โดยค่าของตัวแปรเหล่านี้จะไม่แชร์กันระหว่างวัตถุแต่จะถูกใช้งานตามแต่ละ Object และถูกทำลายเมื่อ Object ถูกทำลาย
using System;
class PersonalInfo {
String name; //ตัวแปรภายนอก Method, Instance Variables
int age;
public static void Main(String[] args) {
PersonalInfo profile1 = new PersonalInfo(); //สร้าง object profile1
profile1.name = "Danudeth";
profile1.age = 20;
PersonalInfo profile2 = new PersonalInfo(); //สร้าง object profile2
profile2.name = "Natchaya";
profile2.age = 21;
Console.WriteLine("object profile1:");
Console.WriteLine(profile1.name);
Console.WriteLine(profile1.age);
Console.WriteLine("object profile2:");
Console.WriteLine(profile2.name);
Console.WriteLine(profile2.age);
}
}
Static Variables
เป็นตัวแปรที่ประกาศจะเป็นคาคงที่ภายในคลาสที่อยู่นอก Method ตัวแปรเหล่านี้จะถูกแชร์ ไม่ต้องใช้ Object ในการเข้าถึงเปลี่ยนแปลงค่าของตัวแปรได้ และรักษาค่าไว้ตลอดทั้งโปรแกรม จะถูกทำลายเมื่อสิ้นสุดโปรแกรม
using System;
class EvenCounter {
static int counter = 0; //ตัวแปรภายนอก Method, Static Variables
static void Main(String[] args) {
Console.WriteLine("counter: " + counter);
counter+=2;
Console.WriteLine("counter: " + counter);
counter+=2;
Console.WriteLine("counter: " + counter);
}
}
Constant Variables
เป็นตัวแปรที่ใช้ const จะสื่อถึงตัวแปรคงที่ โดยจะไม่สามารถแก้ไขได้หลังจากการประกาศตัวแปรได้ตลอดทั้งโปรแกรม โดยไม่ต้องใช้ Object ในการเข้าถึง ช่วยให้อ่านโค้ดได้ง่าย และป้องกันการแก้ไขค่าของตัวแปรโดยไม่ตั้งใจ
using System;
class NotModify {
const int a = 1; //Constant Variables
public static void Main(){
//Program.a++; error ไม่สามารถเปลี่ยนแปลงได้
Console.WriteLine("a: " + NotModify.a);
}
}
Readonly Variables
เป็นตัวแปรที่ไม่สามารถแก้ไขค่าได้ โดยจะถูกใช้งานหลังจากที่มีการสร้าง Object ของคลาสเท่านั้น โดยสามารถ สร้างค่าเริ่มต้นสำหรับตัวแปรที่ไม่ได้ถูกกำหนดไว้ตอนแรกผ่าน Constructor
using System;
class NotModifyUseObject {
readonly int a = 1; //Readonly Variables
readonly int b;
public NotModifyUseObject() { //Constructor ถูกใช้งานเมื่อมีการสร้าง ObjectของClass
this.b = 11;
}
public static void Main() {
NotModifyUseObject ro = new NotModifyUseObject(); //สร้าง Object
Console.WriteLine("a: " + ro.a);
Console.WriteLine("b: " + ro.b);
}
}
Compare with another language
C#
Java
C
Python
ในส่วนที่ไม่มีเครื่องหมาย ✅ ไม่ใช่ว่าไม่มีในภาษานั้น แต่หมายถึง มีการใช้งานผ่านชื่อของการประกาศที่แตกต่างกัน หรือสามารถจำลองการใช้งานได้ และมีการใช้รูปแบบที่เป็นมาตรฐานเพื่อสื่อความหมายที่เป็นสากล โดยมีโค้ดตัวอย่าง ดังต่อไปนี้
ชื่อประเภทในการประกาศตัวแปรที่ต่างกันในแต่ละภาษา
class JavaReadOnly {
public final int readonlyVariable = 50; //Readonly Variable (ผ่าน final)
}
จำลองการใช้งาน และไม่มีให้ใช้งานโดยตรง
class PythonStatic:
static_variable = 10 #Static Variable จำลองการใช้งานได้ แต่ไม่ได้ระบุโดยตรง
Video Presentation
Slide Presentation
Reference
Last updated