📜  C#|关键字词

📅  最后修改于: 2021-05-30 01:18:34             🧑  作者: Mango

关键字或保留字是用于某些内部过程或表示某些预定义动作的语言中的字。因此,不允许将这些单词用作变量名或对象。这样做会导致编译时错误

例子:

// C# Program to illustrate the keywords
using System;
  
class GFG {
  
    // Here static, public, void 
    // are keywords    
    static public void Main () {
          
        // here int is keyword
        // a is identifier
        int a = 10;
          
        Console.WriteLine("The value of a is: {0}",a);
          
        // this is not a valid identifier
  
        // removing comment will give compile time error
        // double int = 10;
         
    }
}

输出:

The value of a is: 10

C#中共有78个关键字,如下所示:

abstract
as
base
bool
break
byte
case
catch
char
checked
class
const
continue
decimal
default
delegate
do
double
else
enum
event
explicit
extern
false
finally
fixed
float
for
foreach
goto
if
implicit
in
int
interface
internal
is
lock
long
namespace
new
null
object
operator
out
override
params
private
protected
public
readonly
ref
return
sbyte
sealed
short
sizeof
stackalloc
static
string
struct
switch
this
throw
true
try
typeof
unit
ulong
unchecked
unsafe
ushort
using
using static
virtual
void
volatile
while

C#中的关键字主要分为以下十类:

  1. 值类型关键字:值类型中15个关键字,用于定义各种数据类型。
    bool byte char decimal
    double enum float int
    long sbyte short struct
    unit ulong ushort

    例子:

    // C# Program to illustrate the
    // value type keywords
    using System;
       
    class GFG {
       
        // Here static, public, void 
        // are keywords    
        static public void Main () {
               
            // here byte is keyword
            // a is identifier
            byte a = 47;
            Console.WriteLine("The value of a is: {0}",a);
               
               
            // here bool is keyword
            // b is identifier
            // true is a keyword
            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. 引用类型关键字:引用类型中6个关键字,用于存储数据或对象的引用。此类别中的关键字是:类,委托,接口,对象,字符串,void
  3. 修饰符关键字:修饰符中17个关键字,用于修改成员类型的声明。
    public private internal protected abstract
    const event extern new override
    partial readonly sealed static unsafe
    virtual volatile

    例子:

    // C# Program to illustrate the
    // modifiers keywords
    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);
      
        }
      
    }
    

    输出:

    Value of n1: 77
  4. 语句关键字:程序指令中总共使用了18个关键字。
    if else switch do for
    foreach in while break continue
    goto return throw try catch
    finally checked unchecked

    例子:

    // 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"); 
            } 
        } 
    } 
    

    输出:

    GeeksforGeeks
  5. 方法参数关键字:共有4个关键字,用于更改传递给方法的参数的行为。此类别中包括的关键字是: params,in,ref,out
  6. 命名空间关键字:在此类别中,共有3个关键字在命名空间中使用。关键字是:名称空间,using,extern
  7. 运算符关键字:共有8个关键字,可用于不同目的,例如创建对象,获取对象的大小等。关键字为: as,is,new,sizeof,typeof,true,false,stackalloc
  8. 转换关键字:类型转换中使用3个关键字。关键字是:显式,隐式,运算符
  9. 访问关键字:2个关键字用于访问和引用该类或该类的实例。关键字是base,this
  10. 字面量关键字:2个关键字分别用作字面量或常量。关键字为null,默认为

重要事项:

  • 关键字不用作类,变量等的标识符或名称。
  • 如果要使用关键字作为标识符,则必须使用@作为前缀。例如, @ abstract是有效的标识符,但不是抽象的,因为它是一个关键字。

例子:

int a = 10;              // Here int is a valid keyword
 
double int = 10.67;     // invalid because int is a keyword

double @int = 10.67;   // valid identifier, prefixed with @

int @null = 0;       // valid
// C# Program to illustrate the use of 
// prefixing @ in keywords
using System;
  
class GFG {
  
    // Here static, public, void 
    // are keywords    
    static public void Main () {
          
        // here int is keyword
        // a is identifier
        int a = 10;
          
        Console.WriteLine("The value of a is: {0}",a);
          
        // prefix @ in keyword int which 
        // makes it a valid identifier
        int @int = 11;
          
        Console.WriteLine("The value of a is: {0}",@int);
         
    }
}

输出:

The value of a is: 10
The value of a is: 11

内容相关关键字

这些用于在程序中赋予特定的含义。每当C#中出现新关键字时,都会将其添加到上下文关键字中,而不是添加到关键字类别中。这有助于避免使用早期版本编写的程序崩溃。

重要事项:

  • 这些不是保留字。
  • 它可以用作上下文之外的标识符,这就是它命名上下文关键字的原因。
  • 在两个或多个上下文中,它们可能具有不同的含义。
  • C#中总共有30个上下文关键字。
add
alias
ascending
async
await
by
descending
dynamic
equals
from
get
global
group
into
join
let
nameof
on
orderby
partial(type)
partial(method)
remove
select
set
value
var
when
where
where
yield

例子:

// C# program to illustrate contextual keywords
using System; 
  
public class Student { 
  
    // Declare name field 
    private string name = "GeeksforGeeks"; 
  
    // Declare name property 
    public string Name 
    { 
  
    // get is contextual keyword
    get
        { 
            return name; 
        } 
          
        // set is a contextual
        // keyword
        set
        { 
            name = value; 
        } 
    } 
} 
  
class TestStudent { 
  
    // Main Method 
    public static void Main(string[] args) 
    { 
        Student s = new Student(); 
  
        // calls set accessor of the property Name, 
        // and pass "GFG" as value of the 
        // standard field 'value'. 
        s.Name = "GFG"; 
  
        // displays GFG, Calls the get accessor 
        // of the property Name. 
        Console.WriteLine("Name: " + s.Name); 
          
        // using get and set as 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

参考: https : //docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/