usingSystem;classGFG{ // static, public, void เป็น KeywordsstaticpublicvoidMain(){ // int เป็น Keywords a เป็น identifier inta=10;Console.WriteLine("The value of a is: {0}",a);}}
HelloWorld.cs(5,9): error CS1525: Unexpected symbol `int'
HelloWorld.cs(5,13): error CS1525: Unexpected symbol `='
ตัวอย่าง:
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
ตัวอย่าง:
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
ตัวอย่าง:
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
เช่น:
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
ตัวอย่าง:
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
#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);
}
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)
| ^~~
class HelloWorld {
public static void main(String[] args)
{
String this = "Hello World!";
System.out.println(this);
}
}
HelloWorld.java:4: error: not a statement
String this = "Hello World!";
^
HelloWorld.java:4: error: ';' expected
String this = "Hello World!";
^
2 errors