02 Handout 1
02 Handout 1
Data Types
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 1, the identifiers are
ConsoleApp, ComputeRectangleArea, length, width, area, WriteLine, and ReadKey.
Code Listing 1. Sample class with identifiers
using System;
namespace ConsoleApp
{
class ComputeRectangleArea
{
static void Main()
{
int length, width, area;
length = 50;
width = 8;
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#:
• 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# must always begin with a capital letter.
The following are the examples of valid and invalid identifiers:
Valid Identifiers Invalid Identifiers
_score score_ 1score 1score is invalid because it begins with number.
first_Name ScoreClass first Name first Name is invalid because it contains white space.
grade1 ComputeScore class class is an invalid identifier because it is a reserved keyword.
Table 1. Example valid identifiers Table 2. Example 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 3 shows the list of 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
Table 3. C# keywords (Harwani, 2015)
From To Example
int long, float, or double int a = 25; double b = a;
//the value of b is 25.0
uint long, ulong, float, or double uint a = 25; long b = a;
long float or double long a = 25; double b = a;
ulong float or double ulong a = 25; double b = a;
float double float a = 25.0F; double b = a;
char ushort, int, uint, long, ulong, float, or double char a = 'a'; int b = a;
//the value of b is 97 because the
decimal value of character 'a' in ASCII
table is 97
Table 5. Implicit numeric conversions (Deitel, P. and Deitel, H., 2015)
Note: There is no implicit conversion of any data type to char type, so the values of the other integral types do not
automatically convert to the char type. The bool and double data types have no possible implicit conversions to the other
data types.
• Explicit conversion. Converting a higher precision data type to a lower precision data type is invalid, and the compiler will
return an error. For example, the statements double num1 = 25.0; int num2 = a; will return an error because the
precision of variable num1 of double data type is higher than the variable num2 of int type. However, explicit conversions
allow the users to convert a value of higher precision data type into a value of lower precision data type by using a cast
operator. The following is the general syntax of performing explicit conversion in C#:
(data_type) data_to_convert;
The (data_type) is the cast operator that will create a converted copy of the value in data_to_convert. For example, the
following statements uses a cast operator to explicitly convert data types:
//first example
int num = 25;
byte b = (byte) num;
//second example
double price = 75.5;
int varA = (int) price; //the value of variable a will be 75
In the first example, the value 25 is assigned to the variable num of int type, then the converted copy of value from variable
num is assigned to the variable b. The value stored in variable num is still an integer type because the cast operator created
a converted copy of its value as byte data type.
Explicit conversion involves the risk of losing information, i.e., from double to int data type. In the second example,
the value of the variable price of double data type is copied and converted into int data type then stored in the variable
varA of int data type. The cast operator dropped the decimal part of the floating-point number 75.5 to convert it to 75 as
int data type.
The following statements shows some of the ways on how to use the cast operator:
int a = (int) 7.9; //the return value of a is 7
double b = (double) (5 + 3); //the value of b is 8.0
Console.WriteLine((int) 2.5); //the output is 2
int c = 64; char d = (char) c; //the return value of d is the character '@'
The following table shows the list of all valid explicit conversions in C#.
From To
sbyte byte, ushort, uint, ulong, or char
byte sbyte or char
short sbyte, byte, ushort, uint, ulong, or char
ushort sbyte, byte, short, or char
int sbyte, byte, short, ushort, uint, ulong, or char
uint sbyte, byte, short, ushort, int, uint, long, ulong, or char
long sbyte, byte, short, ushort, int, uint, ulong, or char
02 Handout 1 *Property of STI
student.feedback@sti.edu Page 4 of 5
IT1907
From To
ulong sbyte, byte, short, ushort, int, uint, long, or char
char sybte, byte, or short
float sbyte, byte, short, ushort, int, uint, long, ulong, or char
double sbyte, byte, short, ushort, int, uint, long, ulong, char, or double
Table 6. Explicit conversions (Deitel, P. and Deitel, H., 2015)
The explicit conversion of a data type to char data type will return the corresponding character of the value from the ASCII
table.
int a = 64; char b = (char) a;
//the value of b is the character '@' because it is the corresponding character of decimal value 64
in ASCII table
REFERENCES:
Deitel, P. and Deitel, H. (2015). Visual c# 2012 how to program, 5th edition. USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual c#, 4th edition. USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in c# 5.0. USA: Cengage Learning PTR
.