Keywords and identifiers in C# programming



C# Keywords

Keywords (sometimes called reserved words) are reserved for use by C# and are always spelled with all lowercase letters. Because they're reserved, they can't be used as identifiers.

Examples of keywords are class , public , or void —they are the names of permanent language elements.

Keywords in C#
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 uint ulong unchecked
unsafe ushort using virtual void
volatile while

The following table contains the list of Contextual Keywords in C# programming language

Contextual Keywords in C#
add alias ascending async await
by descending dynamic equals from
get global group into join
let on orderby partial remove
select set value var where
yield

C# Identifiers

Identifiers are tokens or symbols which are uniquely identify the element. They are names of things like variables or fields. Identifiers are names given to namespaces, classes, methods, variables, interfaces etc.

Identifiers in c# are case sensitive. Identifiers can be combination of letters, numbers and underscores. Keywords are not defined as identifiers. It can make the identifiers by using Unicode character as “@”.

Valid identifiers in C# are following this rule:

  • An identifier must start with a letter or an underscore.
  • After the first character, it may contain numbers, letters, connectors, etc.
  • If the identifier is a keyword, it must be prep ended with “@” .

    Valid Identifiers:
    WORLD
    world
    w_orld
    HelloWorld
    A
    a
    \u098world
    @public
    _world


    Invalid Identifiers:
    //error because 
    first letter is number
    2world  

    //error because it is keyword
    Public  
    
    // error because Unicode 
    formatting character
    \u0027format 

C# Escape sequences

Character combinations consisting of a backslash (\) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences.

The following table outlines the C# escape sequences:

Escape sequence Description
\n Newline. Positions the screen cursor at the beginning of the next line.
\t Horizontal tab. Moves the screen cursor to the next tab stop.
\" Double quote. Used to place a double-quote character (") in a string—e,g., Console.Write( "\"in quotes\"" ); displays "in quotes".
\r Carriage return. Positions the screen cursor at the beginning of the current line—does not advance the cursor to the next line. Any characters output after the carriage return overwrite the characters previously output on that line.
\\ Backslash. Used to place a backslash character in a string.

Displaying Multiple Lines of Text with a Single Statement using escape sequences.


    // Displaying multiple lines with a single statement.
    using System;
    public class EscapeSequences
    {
    // Main method begins execution of C# app
    public static void Main( string[] args )
    {
    Console.WriteLine( "Hello!!!\n I am\n a C# \tProgrammer!!!" );
    } // end Main
    } // end class 

    Hello!!! 
    I am 
    a C#    Programmer!!!

Ads Right