Type of Keyword

กิจจา กิจประเสริฐ 650710526

ใน C# นั้น มี Keywords อยู่ด้วยกันทั้งหมด 78ตัว ซึ่ง สามารถแบ่งกลุ่มออกได้เป็น 10กลุ่มหลัก ๆ ที่มีการเรียกใช้งานที่แตกต่างกัน ได้แก่

1. Value Type Keywords :

Value Type Keyword คือ Keyword ที่ใช้ในการกำหนดชนิดของตัวแปร โดยมีด้วยกันทั้งสิ้น 15ตัว ได้แก่

bool
byte
char
decimal
double

enum

float

int

long

sbyte

short

struct

unit

ulong

short

เปรียบเทียบกับ....

Python : Value Keyword ของ Python หมายถึง Keyword ที่ใช้ในการกำหนดค่าของ boolean ซึ่งค่าพวกนี้จัดเป็น Singleton ที่สามารถเรียกใช้ซ้ำไปซ้ำมาได้เรื่อย ๆ โดยมีอยู่ด้วยกัน 3 ตัว คือ true , false, none

Java : ใน Java เรียก Primitive Data Type มีอยู่ด้วยกัน 8 ตัว byte , short , int , long , float , double , char , boolean

C : ใน C เรียก Primary Data Type มีอยู่ด้วยกัน 4 ตัว ได้แก่ Integer, Floating-point (float), double, string. ทำหน้าที่เหมือนกันกับ data type ใน C# ยกเว้น string

Example :

using System;  
class VTK { 
    static public void Main () { 
        byte a = 47; 
        bool b = true; 
        double c = 0.42e2;
        float d = 134.45E-2f;
        decimal e = 1.5E6m;;
        char g = 'G';
        int h = 10;
        long i = 15000000000L;
        
        Console.WriteLine("The byte of a is: {0}",a);
        Console.WriteLine("The bool of b is: {0}",b);
        Console.WriteLine("The double of c is: {0}",c);
	Console.WriteLine("The float of d is: {0}",d);
        Console.WriteLine("The decimal of e is: {0}",e);
        Console.WriteLine("The char of g is: {0}",g);
        Console.WriteLine("The int of h is: {0}",h); 
        Console.WriteLine("The long of i is: {0}",i);
    } 
}

Output :

The byte of a is: 47
The bool of b is: True
The double of c is: 42
The float of d is: 1.3445
The decimal of e is: 1500000
The char of g is: G
The int of h is: 10
The long of i is: 15000000000

2. Reference Type Keywords :

Reference Type Keyword คือ Keyword ที่ใช้ในการเก็บการอ้างอิงถึงข้อมูลหรือตัว Object ต่างๆ โดย Keywords ในกลุ่มนี้นั้น มีทั้งหมด 6ตัว ได้แก่

class
delegate
interface

object

string

void

เปรียบเทียบกับ....

Python : ใน Python เรียก Declaration Keyword ใช้ในการประกาศตัวแปร ฟังก์ชัน คลาส และ องค์ประกอบอื่น ๆ Keyword ได้แก่ def, class, lambda, global, nonlocal, etc.

Java : ใน Java จะใช้ในการชี้ไปยังตัว Object/ค่าต่าง ๆ คลาส, interface, อาเรย์, enum , annotation จะเก็บ object และค่าต่าง ๆ เอาไว้ในตัวอ้างอิง ซึ่งตัวแปรสามารถเก็บค่าที่เป็น nullได้ด้วย ในการเข้าถึง object ของสมาชิก สามารถทำได้ด้วย dot (.)

C: ใน C เรียกว่า Pointer ใช้ * แทนตัว Pointer(Pointer Variable) เป็นตัวแปรพิเศษในการจัดเก็บตำแหน่งของค่าของตัวแปรต่าง ๆ

Example :

using System;

namespace MyApplication
{
  // Interface
  interface IAnimal 
  {
    void animalSound(); // interface method (does not have a body)
  }

  // Pig "implements" the IAnimal interface
  class Pig : IAnimal 
  {
    public void animalSound() 
    {
      // The body of animalSound() is provided here
      Console.WriteLine("The pig says: wee wee");
    }
  }

  class Program 
  {
    static void Main(string[] args) 
    {
      Pig myPig = new Pig();  // Create a Pig object
      myPig.animalSound();
    }
  }
}

Output :

The pig says: wee wee

3. Modifier Keywords :

Modifier Keywords คือ Keyword ที่ใช้ในการเปลี่ยนแปลง / จำกัด การเข้าถึงตัวของข้อมูลต่าง ๆ Keywords ในกลุ่มนี้มีด้วยกันทั้งสิ้น 17ตัว ได้แก่

public
private
internal
protected
abstract

const

event

extern

new

override

partial

readonly

sealed

static

unsafe

virtual

volatile

เปรียบเทียบกับ....

Python : ใน Python ตัว Modifier Keyword จะใช้กำหนดการเข้าถึงตัวคลาส - public , private , protected

Java : Java แบ่ง modifier ออกเป็น 2 ประเภท ได้แก่ Access Modifier และ Non-Access Modifier โดย Access Modifier จะมี public , private , default , protected ส่วน Non-Access Modifier จะมี final , static, abstract transient, synchronized, volatile

C: ใน C modifier keyword มีเพียง 4 ตัวเท่านั้น ได้แก่ short, long, signed, unsigned เป็น Keyword ที่ใช้ในการปรับเปลี่ยนคุณสมบัติเริ่มต้นของชนิดข้อมูลต่าง ๆ

Example :

using System; 
class Geeks { 
    class Mod 
    {    
        // using public modifier 
        // keyword 
        public int n1; 
    } 
    // Main Method 
    static void Main(string[] args) {  
        Mod obj1 = new Mod(); 
        // access to public members 
        obj1.n1 = 77;  
        Console.WriteLine("Value of n1: {0}", obj1.n1); 
    } 
}

Output :

Value of n1: 77

4. Statements Keywords :

Statements Keywords คือ Keyword ที่ใช้ในการสั่งการให้โปรแกรมทำคำสั่งใด ๆ มีอยู่ด้วยกันทั้งหมด 18ตัว ได้แก่

if
else
switch
do
for

foreach

in

while

break

continue

goto

return

throw

try

catch

finally

checked

unchecked

เปรียบเทียบกับ....

Python : ใน python มี Statement Keyword อยู่ด้วย กัน 6 ตัว ได้แก่ if, while, for, try, with, match และ ยังมี Keyword ย่อยอีก 4 ตัว ได้แก่ except, except*, else, finally โดย 4 ตัวย่อยนี้มักจะใช้อยู่ภายใต้ try

Java : มี Statement 2 ประเภท ได้แก่ decision-making statements (if-then, if-then-else, switch), และ looping statements(for, while, do-while)

C : มีการแบ่ง statements ออกเป็นกลุ่มย่อยหลาย ๆ กลุ่ม ขึ้นอยู่กับ การใช้งาน ดังนี้ if Statement, if-else Statement, Nested if Statement, if-else-if Ladder switch Statement, Conditional Operator, Jump Statements(break, continue, goto, return)

Example :

// C# program to illustrate the statement keywords 
using System;  
class demoContinue  
{  
    public static void Main()  
    { 
        // using for as statement keyword 
        // GeeksforGeeks is printed only 2 times  
        // because of continue statement  
        for(int i = 1; i < 3; i++)  
        {        
            // here if and continue are keywords 
            if(i == 2)  
            continue;            
            Console.WriteLine("GeeksforGeeks");  
        }  
    }  
}

Output :

GeeksforGeeks

5. Method Parameters Keywords :

Method Parameters Keywords คือ Keyword ที่ใช้ในการเปลี่ยนแปลงการกระทำของตัว parameters ต่างๆ ที่ถูกใส่เข้าไปใน methods นั้นๆ Keyword กลุ่มนี้มีอยู่ด้วยกัน 4 ตัว ได้แก่

params

in

ref

out

เปรียบเทียบกับ....

Python : Keyword สำคัญสำหรับการประกาศ method parameter ของ python คือ def ตามด้วย function_name(parameter)

Java : ใน Java มีความคล้ายคลึงกันกับ C# แต่ใน Java จะส่ง parameters เป็นแบบ value เสมอ

C: ใน C method parameter จะต้องประกอบไปด้วย returnType functionName(parameter1, parameter2, ...){}

Example :

// C# program to illustrate the// concept of the named parameters 
using System; 
public class DNT { 
// addstr contain three parameters 
public static void addstr(string s1, string s2, string s3) 
{ 
 string result = s1 + s2 + s3; 
 Console.WriteLine("Final string is: " + result); 
} 
// Main Method 
static public void Main() 
{ 
 // calling the static method with named 
 // parameters without any order 
 addstr(s1: "Dot", s2: "Net", s3: "Tricks"); 
 } 
}

Output :

Final string is: DotNetTricks

6. Namespace Keywords :

Namespace Keywords คือ Keyword ที่ใช้ในการประกาศขอบเขตที่จะใช้ในการจัดเก็บกลุ่มของ Object ที่มีความเกี่ยวข้องกันไว้ใน Namespace ใด ๆ ทั้งนี้ Object / ชื่อคลาส ต่าง ๆ ที่ถูกประกาศไว้ใน namespace ใด ๆ แล้ว จะไม่เกิดการขัดแย้งต่อ Object / ชื่อคลาส ที่เหมือนกัน ที่ถูกประกาศไว้ใน namespace อื่น โดยเราสามารถใช้ namespace ในการจัดระเบียบของโค้ดและสร้างประเภทเฉพาะของตัว global โดยมี Keyword ที่ใช้อยู่ด้วยกันทั้งหมด 3 ตัว ได้แก่

namespace

using

extern

เปรียบเทียบกับ....

Python : ใน Python สามารถแบ่ง namespace ออกได้เป็น 4 ประเภท ได้แก่ Built-in, Global, Enclosing, Local ซึ่ง namespace เหล่านี้จะเปรียบเสมือนตัว dictionary

Java : ใน Java. namespace สามารถสร้างขึ้นมาได้ โดยการเรียกใช้ package keyword ตามด้วยชื่อ "namespace" ที่ต้องการ

C : ไม่มี namespace แต่มี scope ที่มีแนวคิดใกล้เคียงกับ namespace

Example :

using System;  
using First;  
using Second;  
namespace First {  
public class Hello  
{  
    public void sayHello() { Console.WriteLine("Hello Namespace"); }  
}  
}  
namespace Second  
{  
    public class Welcome  
    {  
        public void sayWelcome() { Console.WriteLine("Welcome Namespace"); }  
    }  
}  
public class TestNamespace  
{  
    public static void Main()  
    {  
        Hello h1 = new Hello();  
        Welcome w1 = new Welcome();  
        h1.sayHello();  
        w1.sayWelcome();  
    }  
}  

Output :

Hello Namespace
Welcome Namespace

7. Operator Keywords :

Operator Keywords คือ Keyword ที่มีการใช้งานที่แตกต่างกันขึ้นอยู่กับวัตถุประสงค์ที่ต้องการจะให้เป็น โดยอาจจะเป็นการสร้าง Object , รับขนาดของ Object มีอยู่ด้วยกันทั้งสิ้น 8 ตัว ได้แก่

as

is

new

sizeof

typeof

true

false

stackalloc

เปรียบเทียบกับ....

Python : ใน Python มี Operator อยู่ด้วยกัน 5 ตัว ได้แก่ and, or, not, in, is ซึ่ง 3 ตัวแรก จะทำหน้าที่เหมือนกับสัญลักษณ์ทางตรรกศาสตร์ในเชิงคณิตศาสตร์

Java : Java ไม่มี Operator Keyword ที่แน่ชัด แต่สนับสนุนการตัวดำเนินการที่หลากหลายแทน แบ่งออกได้เป็น 8 กลุ่ม - Unary, Arithmetic, Shift, Relational, Bitwise, Logical, Ternary, Assignment Operator

C : C มี Operator ที่ค่อนข้างคล้ายกับ Java เพียงแต่ C จะไม่มี Shift Operator และมี Sizeof , Comma, Member Selection Operator เพิ่มเข้ามา

using System; 
using System.IO; 
using System.Text; 
namespace IncludeHelp 
{ 
    class Test 
    { 
        // Main Method  
        static void Main(string[] args) 
        { 
            Console.WriteLine("sizeof(char)     : {0}", sizeof(char)); 
            Console.WriteLine("sizeof(byte)     : {0}", sizeof(byte)); 
            Console.WriteLine("sizeof(sbyte)    : {0}", sizeof(sbyte)); 
            Console.WriteLine("sizeof(float)    : {0}", sizeof(float)); 
            Console.WriteLine("sizeof(ushort)   : {0}", sizeof(ushort)); 
            Console.WriteLine("sizeof(double)   : {0}", sizeof(double)); 
            Console.WriteLine("sizeof(int)      : {0}", sizeof(int)); 
            Console.WriteLine("sizeof(bool)     : {0}", sizeof(bool)); 
            Console.WriteLine("sizeof(short)    : {0}", sizeof(short));      
        } 
    } 
} 

Output :

sizeof(char)     : 2
sizeof(byte)     : 1
sizeof(sbyte)    : 1
sizeof(float)    : 4
sizeof(ushort)   : 2
sizeof(double)   : 8
sizeof(int)      : 4
sizeof(bool)     : 1
sizeof(short)    : 2

8. Conversion Keywords :

Conversion Keywords คือ Keyword ที่ใช้ในการแปลงประเภทของข้อมูลไปเป็นอีกประเภทหนึ่ง มี Keyword ด้วยกันทั้งสิ้น 3 ตัว ได้แก่

explicit

implicit

operator

เปรียบเทียบกับ....

Python : มีการ Conversion อยู่ 2 รูปแบบ - Implicit Conversion หรือ การ Conversion เองโดยอัตโนมัติของตัวโค้ดเอง และ Explicit Conversion หรือ การ Conversion ด้วยตัวเองโดยการเขียนโค้ดกำหนดการแปลงลงไป

Java : มีการ Conversion 4 รูปแบบ - Widening (Automatic), Narrowing (Explicit), Type Promotion in Expression และ Explicit Type Casting in Expressions

C : C มีรูปแบบการ Conversion อยู่ 2 รูปแบบ เช่นเดียวกันกับ Python คือ Implicit และ Explicit Conversion

Example :

using System;
namespace MyApplication {
  class Program {
    static void Main(string[] args) {
      int numInt = 500;
      // get type of numInt
      Type n = numInt.GetType();
      // Implicit Conversion
      double numDouble = numInt;
      // get type of numDouble
      Type n1 = numDouble.GetType();
      // Value before conversion
      Console.WriteLine("numInt value: "+numInt);
      Console.WriteLine("numInt Type: " + n);
      // Value after conversion
      Console.WriteLine("numDouble value: "+numDouble);
      Console.WriteLine("numDouble Type: " + n1);
      Console.ReadLine();
    }
  }
}

Output :

numInt value: 500
numInt Type: System.Int32
numDouble value: 500
numDouble Type: System.Double

9. Access Keywords :

Access Keywords คือ Keyword ที่ใช้ในการเข้า/อ้างอิงถึง คลาสหรือ instance ของคลาสใด ๆ มี Keyword ทั้งหมด 2 ตัว ได้แก่

base

this

เปรียบเทียบกับ....

Python : ใน Python ไม่มี Access Keyword ที่เจาะจงเฉพาะแน่นอน

Java : ใน Java Access เกี่ยวข้องกับ ตัว Modifier หรือก็คือ ใช้ Keyword เดียวกัน ซึ่งก็คือ public, private, default , protected

C++ : ใน C++ มี Keyword ที่ใช้ในการ Access เช่นเดียวกันกับ Java เพียงแต่จะไม่มีตัว default เท่านั้น

Example :

using System;  
public class Animal{  
    public string color = "white";  
}  
public class Dog: Animal  
{  
    string color = "black";  
    public void showColor()  
    {  
        Console.WriteLine(base.color);  
        Console.WriteLine(color);  
    }  
}  
public class TestBase  
{  
    public static void Main()  
    {  
        Dog d = new Dog();  
        d.showColor();  
    }  
}  

Output :

white
black

10. Literal Keywords :

Literal Keywords คือ Keyword ที่ใช้ในแทนตัวอักษรหรือค่าคงที่ มีอยู่ด้วยกัน 2 ตัว ได้แก่

null

default

เปรียบเทียบกับ....

Python : ใน Python literal นับเป็นตัวแทนของค่าที่ถูกแก้ไขแล้วในโปรแกรม สามารถอยู่ในรูปแบบใดก็ได้ ไม่ว่าจะเป็น ตัวเลข ตัวอักษร หรือ ประโยค

Java : ใน Java. Literal คือ ค่าทุกอย่างที่ถูกกำหนดไว้ให้กับตัวแปรต่าง ๆ เช่น boolean, numeric, character, string จัดได้ 5 ประเภท คือ Integer(Decimal literals (Base 10), Octal literals (Base 8), Hexa-decimal literals (Base 16), Binary literals), Floating-point (Decimal literals(Base 10)), Char(Single quote, Char literal as Integral literal, Unicode Representation, Escape Sequence), String และ boolean literal

C : มี Literal อยู่ด้วยกัน 4 ประเภท ได้แก่ Integer, Float Character และ String Literal ส่วนของ Integer Literal จะแบ่ง ได้อีก 4 แบบย่อย คือ Decimal-literal(base 10) , Octal-literal(base 8) , Hex-literal(base 16) , Binary-literal(base 2)

Example :

using System;
class Geeks{
    // Main method
    public static void Main(String[] args)
    { 
        // decimal-form literal
        int a = 101;
        // Hexa-decimal form literal
        int c = 0xFace;
        // binary-form literal
        int x = 0b101;
        Console.WriteLine(a);
        Console.WriteLine(c);
        Console.WriteLine(x);
    }
}

Output :

101
64206
5

Video :

---

Slide :

---

Reference :

C# Keyword & Code :

Python Keyword Reference & Code :

Java Keyword Reference & Code :

C Keyword Reference & Code :

C++ Keyword Reference & Code :

Last updated