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; 
	} 
}
ผลลัพธ์:
HelloWorld.cs(5,9): error CS1525: Unexpected symbol `int'
HelloWorld.cs(5,13): error CS1525: Unexpected symbol `='

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

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

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

ตัวอย่าง:
using System; 
class Geeks { 
	class Mod 
	{ 
		// การใช้ keywords public modifier 
		public int n1; 
	} 
	// เมธอดหลัก
	static void Main(string[] args) { 
		Mod obj1 = new Mod(); 
		// การเข้าถึงสมาชิกที่เป็น public 
		obj1.n1 = 77; 
		Console.WriteLine("Value of n1: {0}", obj1.n1); 
	} 
}
ผลลัพธ์:
Value of n1: 77

4. Statements Keywords

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

if

else

switch

do

for

foreach

in

while

break

continue

goto

return

throw

try

catch

finally

checked

unchecked

ตัวอย่าง:
using System; 
class demoContinue 
{ 
	public static void Main() 
	{	 
		// การใช้ for เป็น Statements Keywords 
		// GeeksforGeeks จะแสดงผล 2 ครั้ง 
		// แค่เพราะคำสั่ง continue ทำให้แสดงครั้งเดียว
		for(int i = 1; i < 3; i++) 
		{ 
			// ที่นี่ if และ continue เป็น Keywords 
			if(i == 2) 
			continue; 
			Console.WriteLine("GeeksforGeeks"); 
		} 
	} 
}
ผลลัพธ์:
GeeksforGeeks

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

เช่น:
int a = 10; // ใช้ int ถูกต้อง เป็น Keywords ที่ใช้กำหนดประเภทของตัวแปร
 
double int = 10.67; // ไม่ถูกต้องเพราะ int เป็น Keywords 

double @int = 10.67; // identifier ที่ถูกต้อง เพราะใช้ @ นำหน้า int

int @null = 0; // ถูกต้อง
ตัวอย่าง:
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); 
		// ใช้ @ หน้า int
		int @int = 11; 
		Console.WriteLine("The value of a is: {0}",@int); 
	} 
} 
ผลลัพธ์:
The value of a is: 10
The value of a is: 11

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

ตัวอย่าง:
using System; 
public class Student { 
	// ประกาศฟิลด์ชื่อ name 
	private string name = "GeeksforGeeks"; 
	// ประกาศ property name 
	public string Name 
	{ 
		// get เป็น Keywords แบบ contextual 
		get
		{ 
			return name; 
		} 
		// set เป็น Keywords แบบ contextual 
		set
		{ 
			name = value; 
		} 
	} 
} 

class TestStudent { 
	// เมธอดหลัก (Main Method) 
	public static void Main(string[] args) 
	{ 
		Student s = new Student(); 
		// เรียกใช้ accessor ของ set ของ property Name, 
		// และส่ง "GFG" เป็นค่าของฟิลด์ 
		// มาตรฐาน 'value'. 
		s.Name = "GFG"; 
		// แสดงผล GFG, เรียกใช้ accessor ของ get 
		// ของ property Name. 
		Console.WriteLine("Name: " + s.Name); 
		// การใช้ get และ set เป็น identifier 
		int get = 50; 
		int set = 70; 
		Console.WriteLine("Value of get is: {0}",get); 
		Console.WriteLine("Value of set is: {0}",set); 
	} 
} 
ผลลัพธ์:
Name: GFG
Value of get is: 50
Value of set is: 70

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

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

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

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

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

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

#include <stdio.h>
void register(int, int);
int main () {
   int a=5, b=7;
   register(a,b);
   return 0;
}
void register(int a, int b)
{
   printf("%d", a+b);
}

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

Main.c:2:15: error: expected identifier or ‘(’ before ‘int’
    2 | void register(int, int);
      |               ^~~
Main.c: In function ‘main’:
Main.c:5:14: error: expected ‘)’ before ‘,’ token
    5 |    register(a,b);
      |              ^
      |              )
Main.c: At top level:
Main.c:8:15: error: expected identifier or ‘(’ before ‘int’
    8 | void register(int a, int b)
      |               ^~~

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

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