L2. C Sharp Programming Comments, Keywords, Identifiers, Variables
L2. C Sharp Programming Comments, Keywords, Identifiers, Variables
Program Structure
Virtually all structured programs share a similar overall pattern:
➔ Statements to establish the start of the program
➔ Variable declaration
➔ Program statements (blocks of code)
2
C# - Comments
★ Comments can be used to explain C# code, and to make it more readable.
★ It can also be used to prevent execution when testing alternative code.
★ Two Types of Comments in C#:
○ Single-line Comments
■ Single-line comments start with two forward slashes (//).
■ Any text between // and the end of the line is ignored by C# (will not be
executed).
■ This example uses a single-line comment before a line of code:
3
C# - Comments
○ Multi-line Comments
■ Multi-line comments start with /* and ends with */.
■ Any text between /* and */ will be ignored by C#.
■ This example uses a multi-line comment (a comment block) to explain the
code:
4
C# - Keywords
➔ Keywords are predefined sets of reserved words that have special meaning in a
program.
➔ The meaning of keywords can not be changed, neither can they be directly used as
identifiers in a program.
➔ For example,
◆ long mobileNum;
● Here, long is a keyword and mobileNum is a variable (identifier). long
has a special meaning in C# i.e. it is used to declare variables of type
long and this function cannot be changed.
➔ Also, keywords like long, int, char, etc. can not be used as identifiers. So, we
cannot have something like:
◆ long long;
➔ C# has a total of 79 keywords.
5
6
7
C# - Keywords
8
C# - Identifiers
10
C# - Identifiers
11
Example: Find list of keywords and identifiers in a program
12
Example: Find list of keywords and identifiers in a program
13
C# - Variables
➔ Every variable has a name, called the variable name, and a data
type. A variable's data type indicates what type of value the
variable represents, such as whether it is an integer, a floating-
point number, or a character.
14
Rules for Naming Variables in C#
There are certain rules we need to follow while naming a variable. The rules for
naming a variable in C# are:
15
16
Best Practices for Naming a Variable
1. Choose a variable name that make sense. For example, name, age,
subject makes more sense than n, a and s.
2. Use camelCase notation (starts with lowercase letter) for naming
local variables. For example, numberOfStudents, age, etc.
3. Use PascalCase or CamelCase (starts with uppercase letter) for
naming public member variables. For example, FirstName, Price,
etc.
4. Use a leading underscore (_) followed by camelCase notation for
naming private member variables. For example, _bankBalance,
_emailAddress, etc.
17
Practice Session
Using System
nameSpace Practice ;
{
class Practice Problem ;
Static void main ( String args )
{
Console . writeline ( “ Practice
Problem ” )
}
};
1. Identify the errors and correct them.
2. What are the keywords and identifiers?
18
Practice Session Solution
using System ;
namespace Practice Keywords Identifiers
{ using System
class PracticeProblem namespace Practice
{
class PracticeProblem
static void Main ( string [] args )
{ static Main
20
How to declare variables in C#?
22
Data Types
23
Data Description Storage Value Example
Type size range
void Used to denote the type with
no values
int Used to denote an integer 2 bytes -32768 to Example: 10, -10
type. 32767 {
int age;
age = 20;
}
Structure A collection of related variables of the same and/or different data types. The
structure is called a record and the variables in the record are called members or
fields.
25
26
C# Literals
Let's look at the following statement:
int number = 41;
Here,
★ int is a data type
★ number is a variable and
★ 41 is a literal
Literals are fixed values that appear in the program. They do not
require any computation. For example, 5, false, 'w' are literals that
appear in a program directly without any computation.
27
Practice Session
1. Assign two variables with the values in the different class (one
variable in one class) inside the same namespace and display
these variables.
28