Keywords

วิโรจ แดงสงฆ์ 650710221

Keywords ใน C#

Keywords (คำสำคัญ) หรือ Reserved words (คำสงวน) คือคำในภาษา C# ที่ใช้สำหรับกระบวนการภายในบางอย่างหรือแสดงถึงการกระทำที่กำหนดไว้ล่วงหน้า คำเหล่านี้จึงไม่อนุญาติให้ใช้ในการตั้งเป็นชื่อ identifier (ตัวระบุ) ของตัวแปรหรือชื่อของออปเจ็ค

ตัวอย่าง:
using System; 
class GFG { 
	// static, public, void เป็น Keywords
	static public void Main () { 
		// int เป็น Keywords a เป็น identifier 
		int a = 10; 
		Console.WriteLine("The value of a is: {0}",a); 
	} 
}
ผลลัพธ์:
The value of a is: 10

หากนำ Keywords มาใช้ตั้งชื่อจะทำให้เกิดข้อผิดพลาดตอนคอมไพล์

ตัวอย่าง:
using System; 
class GFG {  
	static public void Main () { 
		// ตั้งชื่อตัวแปรด้วย Keywords
		double int = 10; 
	} 
}

Keywords ทั้งหมดใน C#

มีทั้งหมด 78 คำดังนี้:

abstract

do

in

protected

throw

as

double

int

public

true

base

else

interface

readonly

try

bool

enum

internal

ref

typeof

break

event

is

return

unit

byte

explicit

lock

sbyte

ulong

case

extern

long

sealed

unchecked

catch

false

namespace

short

unsafe

char

finally

new

sizeof

ushort

checked

fixed

null

stackalloc

using

class

float

object

static

using static

const

for

operator

string

virtual

continue

foreach

out

struct

void

decimal

goto

override

switch

volatile

default

if

params

this

while

delegate

implicit

private

ประเภทของ Keywords

สามารถแยกเป็น 10 ประเภทดังนี้

1. Value Type Keywords

มี Keywords 15 คำในประเภทค่าซึ่งใช้เพื่อกำหนดประเภทข้อมูลต่างๆ ดังนี้

bool

byte

char

decimal

double

enum

float

int

long

sbyte

short

struct

unit

ulong

ushort

2. Reference Type Keywords

มี Keywords 6 คำในประเภทการอ้างอิงซึ่งใช้ในการเก็บข้อมูลอ้างอิงของข้อมูลหรือวัตถุดังนี้

class

delegate

interface

object

string

void

3. Modifiers Keywords

มี Keywords 17 คำในตัวปรับเปลี่ยนซึ่งใช้เพื่อปรับเปลี่ยนคำประกาศของสมาชิกประเภทดังนี้

public

private

internal

protected

abstract

const

event

extern

new

override

partial

readonly

sealed

static

unsafe

virtual

volatile

4. Statements Keywords

มี Keywords ทั้งหมด 18 คำที่ใช้ในคำสั่งโปรแกรมดังนี้

if

else

switch

do

for

foreach

in

while

break

continue

goto

return

throw

try

catch

finally

checked

unchecked

5. Method Parameters Keywords

มี Keywords ทั้งหมด 4 คำที่ใช้เปลี่ยนพฤติกรรมของพารามิเตอร์ที่ส่งผ่านไปยังเมธอดดังนี้

params

in

ref

out

6. Namespace Keywords

มี Keywords ทั้งหมด 3 คำในหมวดหมู่นี้ที่ใช้ในเนมสเปซคำหลักเหล่านี้ดังนี้

namespace

using

extern

7. Operator Keywords

มีทั้งหมด 8 คำซึ่งใช้เพื่อวัตถุประสงค์ต่างๆ เช่น การสร้างอ็อบเจ็กต์ การกำหนดขนาดของอ็อบเจ็กต์ ฯลฯ keywords เหล่านี้ดังนี้

as

is

new

sizeof

typeof

true

false

stackalloc

8. Conversion Keywords

มี Keywords 3 คำที่ใช้ในการแปลงประเภท ดังนี้

explicit

implitcit

operator

9. Access Keywords

คำ Keywords 2 คำที่ใช้ในการเข้าถึงและอ้างอิงคลาสหรือ Instance ของคลาสดังนี้

base

this

10. Literal Keywords

มี คำหลัก 2 คำที่ใช้เป็นตัวอักษรหรือค่าคงที่ดังนี้

null

default

ประเด็นสำคัญ

  • ไม่ใช้ Keywords หรือ Contextual Keywords เป็นชื่อของคลาส หรือ ตัวแปร ฯลฯ

  • หากต้องการใช้ Keywords เป็น identifier ต้องใช้ @ เป็นคำนำหน้า เช่น @abstract ใช้ตั้งชื่อเป็น identifier ได้ แต่ใช้ abstract ไม่ได้เนื่องจากเป็น Keywords

Contextual Keywords

Contextual Keywords (คำสำคัญตามบริบท) ใช้เพื่อให้ความหมายที่เฉพาะเจาะจงในโปรแกรม เมื่อมี Keywords ใหม่ปรากฏใน C# Keywords นั้นจะถูกเพิ่มลงใน Contextual Keywords ไม่ใช่ในหมวดหมู่ของ Keywords วิธีนี้ช่วยหลีกเลี่ยงการเกิดข้อผิดพลาดของโปรแกรมที่เขียนในเวอร์ชันก่อนหน้าได้

ประเด็นสำคัญ

  • Contextual Keywords ไม่ใช่ Keywords

  • สามารถใช้ตั้งเป็นชื่อ identifier ภายนอกบริบทได้ เลยเป็นสาเหตุว่าทำไมตั้งชื่อว่า Contextual Keywords

  • อาจมีความหมายต่างกันได้ในสองบริบทหรือมากกว่า

Contextual Keywords ทั้งหมดใน C#

มีทั้งหมด 30 คำ

add

equals

nameof

value

alias

from

on

var

ascending

get

orderby

when

async

global

partial(type)

where

await

group

partial(method)

where

by

into

remove

yield

descending

join

select

dynamic

let

set

ความแตกต่างระหว่าง Keywords กับ Contextual keywords

Keywords ไม่สามารถใช้ตั้งชื่อของตัวแปรหรือฟังก์ชันได้ เพราะแต่ละคำมีความหมายและหน้าที่เฉพาะ

ส่วน Contextual keywords สามารถใช้ในบริบทอื่นได้ และจะเป็น keyword เมื่อใช้ในสถานการณ์เฉพาะ

เปรียบเทียบกับภาษา C, Java, Python

เมื่อนำ C# มาเปรียบเทียบกับภาษาอื่น จะพบว่ามีความเหมือนและแตกต่างดังนี้

ตัวอย่างการตั้งชื่อที่ผิดด้วย Keywords

ผลลัพธ์: เกิดข้อผิดพลาดเพราะใช้ register ซึ่งเป็น Keywords มาตั้งเป็นชื่อของฟังก์ชัน

Keywords ทั้งหมดในแต่ละภาษา:

มี 32 คำดังนี้

auto

double

int

struct

break

else

long

switch

case

enum

register

typedef

char

extern

return

union

continue

for

signed

void

do

if

static

while

default

goto

sizeof

volatile

const

float

short

unsigned

Contextual Keywords ภาษา C, Java, Python

ไม่มี Contextual Keywords อย่างเป็นทางการ


Presentation

Slide

594KB
Open

Video


Reference

ในส่วนของ C# Keywords และ Contextual keywords

GeeksforGeeks. (2019, January 21). C# | Keywords. GeeksforGeeks.https://www.geeksforgeeks.org/c-sharp-keywords/

BillWagner. (2024a, August 14). C# Keywords and contextual keywords - C# reference. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/

ในส่วนของการเปรียบเทียบกับภาษา C, Java, Python

โค้ดและผลลัพธ์ในการตั้งชื่อที่ผิดด้วย Keywords

C - keywords. (n.d.-b). https://www.tutorialspoint.com/cprogramming/c_keywords.htm

GeeksforGeeks. (2024, April 22). Java keywords. GeeksforGeeks.https://www.geeksforgeeks.org/list-of-all-java-keywords/

Python keywords and identifiers. Programiz. (n.d.). https://www.programiz.com/python-programming/keywords-identifier

ในส่วนของ Keywords ของแต่ละภาษา

C - keywords. Tutorialspoint. (n.d.-a). https://www.tutorialspoint.com/cprogramming/c_keywords.htm

Java language keywords. Java Language Keywords (The JavaTM Tutorials > Learning the Java Language > Language Basics). (n.d.). https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

2. lexical analysis. Python documentation. (n.d.). https://docs.python.org/3/reference/lexical_analysis.html#keywords

ในส่วนของ Java Contextual Keywords

Contextual keywords. (n.d.). https://docs.oracle.com/javase/specs/jls/se16/preview/specs/contextual-keywords-jls.html

Last updated