TOC

This article is currently in the process of being translated into Thai (~97% done).

Classes:

Constants (the const keyword)

เราได้เรียนเกี่ยวกับตัวแปรมาหลายตัวแล้ว สิ่งที่ตรงกันข้ามกับตัวแปรก็คือ constant (keyword const) เราจะใช้ เมื่อเราต้องการค่าคงตัว ที่ไม่มีวันเปลี่ยนแปลง เราจะใช้ const นี้เพื่อป้องกันไม่ให้ค่านั้นเปลี่ยนโดยไม่ได้ตั้งใจ

เราจะพบ const ใน framework มากมาย เช่น

Console.WriteLine(Math.PI);

เราสามารถตั้งค่า const ใน method ได้ดังนี้

static void Main(string[] args)
{
    const int TheAnswerToLife = 42;
    Console.WriteLine("The answer to life, the universe and everything: " +  TheAnswerToLife);
}

Const ส่วนใหญ่จะตั้งค่าใน class level ซึ่งจะเข้าถึงได้ก็ขึ้นอยู่กับ visibility const จะคล้ายๆ static method คือ ไม่ต้อง สร้าง instant object

using System;

namespace Constants
{
    class Program
    {
static void Main(string[] args)
{
    Console.WriteLine("The fake answer to life: " + SomeClass.TheFakeAnswerToLife);
    Console.WriteLine("The real answer to life: " +  SomeClass.GetAnswer());
}
    }

    class SomeClass
    {
private const int TheAnswerToLife = 42;
public const int TheFakeAnswerToLife = 43;

public static int GetAnswer()
{
    return TheAnswerToLife;
}
    }
}

เราได้สร้าง class ชื่อ SomeClass ที่เราตั้งค่า const ไว้ 2 ค่า (private & public) ใน main program เราจะเรียกใช้ const ได้โดยตรง แต่ const อีกตัวนึง เราต้องใช้ GetAnswer() เข้ามาช่วย

เราจะใช้ค่าแบบไหนเป็นค่าคงที่? (constant)

Const จะต้องตั้งค่าทันทีที่เริ่มต้น compiler ต้องสามารถประมวลผลได้เลย เพราะฉะนั้นจะใช้ค่าของ integer, float, string, และ Boolean ได้ แต่จะใช้ DateTime ใน const ไม่ได้

เมื่อ compiler ต้องสามารถประมวลผลได้เลย เพราะฉะนั้น const ก็จะมีข้อจำกัดในการตั้งค่าเริ่มต้น ตัวอย่างด้านล่างเป็นตัวอย่างที่ใช้ const ได้

const int a = 10;  
const float b = a * 2.5f;

const string s1 = "Hello, world!";  
const string s2 = s1 + " How are you?";

เราไม่สามารถใช้คำตอบ (return value) ของ method มาตั้งค่า const ได้ ตัวอย่างด้านล่างเป็นตัวอย่างที่ใช้ const ไม่ได้

// NOT possible:
const int a = Math.Cos(4) * 2;
// Possible:
const string s1 = "Hello, world!";
// NOT possible:
const string s2 = s1.Substring(0, 6) + " Universe";

Compiler จะต้องได้ค่าไปประมวลผลเลย ไม่ใช่ต้องประมวลผลก่อน

Constant ทางเลือก (readonly field)

ถ้าเราต้องการ constant ที่ไม่จำกัดการตั้งค่ามากนัก เราจะใช้ readonly keyword จะใช้ใน method ไม่ได้ แต่จะใช้ใน class ได้ ค่าที่ตั้งไว้แล้ว สามารถเปลี่ยนใน constructor ได้ หลังจากนั้น readonly field จะไม่สามารถเปลี่ยนค่าได้

class SomeClass
{
    private readonly DateTime rightNow;
    public readonly DateTime later = DateTime.Now.AddHours(2);

    public SomeClass()
    {
this.rightNow = DateTime.Now;
    }
}

ในตัวอย่างเรามี 2 readonly field ที่เป็น private และ public ตัวแรก เราไม่ได้ตั้งค่าให้ ส่วนตัวที่ 2 เราตั้งค่าให้เลย

ใน class SomeClass เราได้ตั้งค่าของ rightNow ใน constructor อย่างที่ได้กล่าวไว้ข้างต้นว่า นอกจากใน constructor แล้ว readonly จะไม่สามารถเปลี่ยนค่าได้อีก compiler จะเตือนว่ามีข้อผิดพลาดเกิดขึ้น

สรุป

Const สามารถตั้งค่าได้ใน method หรื อ class ซึ่งจะใช้แทนค่าคงที่ ที่เรารู้แล้วและจะไม่มีการเปลี่ยนแปลงเมื่อ compile ค่าส่วนใหญ่จะเป็น integer, float, string, และ Boolean ถ้าจะให้ใช้ได้อย่างยึดหยุ่นเปลี่ยนแปลงได้บ้าง เราจะใช้ readonly field


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!