Identifiers and Keywords
Identifiers and Keywords
An identifier is a name of a program component programmers use to uniquely identify namespaces, classes, methods, variables, constants, etc. Identifiers are user-defined words. For example, in the program shown in Code Listing one, the identifiers are ConsoleApp, ComputeRectangleArea, length, width, area, WriteLine, and ReadKey.
Code Listing one. Sample class with identifiers using System; namespace ConsoleApp {
class ComputeRectangleArea {
static void Main() { int length, width, area; length = fifty; width = eight; area = length * width; Console.WriteLine("The area of the rectangle is " + area); Console.ReadKey();
}
The following are the syntax rules when naming an identifier in C sharp:
. It must start with a letter of the English alphabet or an underscore character.
. The identifier's name can only have any combination of letters, digits, and underscores. White spaces are not allowed.
. Identifiers are case sensitive. For example, the identifier Area is not the same with identifiers area or AREA.
. It cannot be a reserved keyword.
. The classes and methods in C sharp must always begin with a capital letter.
The following are the examples of valid and invalid identifiers:
Keywords are reserved words a programming language uses for its own use, and they have a special predefined meaning to the compiler. These cannot be used as identifiers. If you do use a keyword in a program, the compiler will throw an error message. Table three shows the list of keywords in C sharp.
Variables
Variables
A variable is an identifier and a memory location that stores a specific value. Variables hold a value that can be changed during program execution. For example, a variable named score assigned an initial value of twenty-five. When the program starts, the value of variable score will change to eighty-five. The basic syntax of declaring a variable is as follows:
data_type identifier;
The data_type is one of C sharp's data types, and the identifier is the name of the variable. For example, int score; Variables are initialized, or assigned a value, with an equal sign followed by the value. The following are some valid examples of declaring and initializing variables:
. You can initialize a variable at the time of declaration. For example:
int score = twenty-five;
. You can declare and initialize more than one variable of the same time data type using a comma in a single statement: int score, age; score = eighty-five; age = twenty-four; int length = eight, width = five;
When creating a program, you must define a variable with a meaningful name that is easy to understand what value must store on it. For example, the variables gameScore and age must store integer type values. Use camelCase notation that starts with a lowercase letter for naming local variables. For example, numberOfStudents, age, totalPrice, etc.