Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
20 views

L2. C Sharp Programming Comments, Keywords, Identifiers, Variables

The document discusses key concepts in C# programming including comments, keywords, identifiers, variables, data types, and best practices for naming variables. It provides examples and explanations of single-line and multi-line comments, lists some common C# keywords, and outlines rules for naming identifiers and variables. It also defines variables and data types, listing some common built-in C# data types like int, char, and float along with their descriptions and example values.

Uploaded by

fiadhasan102
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

L2. C Sharp Programming Comments, Keywords, Identifiers, Variables

The document discusses key concepts in C# programming including comments, keywords, identifiers, variables, data types, and best practices for naming variables. It provides examples and explanations of single-line and multi-line comments, lists some common C# keywords, and outlines rules for naming identifiers and variables. It also defines variables and data types, listing some common built-in C# data types like int, char, and float along with their descriptions and example values.

Uploaded by

fiadhasan102
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

C # Programming

Variables, Data types, Keywords, Identifiers, Comments


Program
As a programmer, after problem solving phase, next step is to code
the program-that is, to express our solution in a programming
language. We will translate the logic from the algorithm, flowchart
or pseudocode-or some other tool to a programming language.

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

➔ Although keywords are reserved words, they can be used as identifiers


if @ is added as prefix. For example,
◆ int @void;

8
C# - Identifiers

➔ Identifiers are the name given to entities such as variables, methods,


classes, etc.
➔ They are tokens in a program which uniquely identify an element. For
example,
◆ int value;
● Here, value is the name of variable. Hence it is an
identifier.
➔ Reserved keywords can not be used as identifiers unless @ is added as
prefix. For example,
◆ int break;
● This statement will generate an error in compile time.
9
Rules for Naming an Identifier

★ An identifier can not be a C# keyword.


★ An identifier must begin with a letter, an underscore or @
symbol.
○ The remaining part of identifier can contain letters, digits and
underscore symbol.
★ Whitespaces are not allowed. Neither it can have symbols other
than letter, digits and underscore.
★ Identifiers are case-sensitive. So, getName, GetName and
getname represents 3 different identifiers.

10
C# - Identifiers

➔ Here are some of the valid and invalid 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

➔ In computer programming, a variable is a storage location and an


associated symbolic name which contains some known or
unknown quantity or information, a value. It is helpful to think of
variables as containers that hold information.

➔ 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:

➔ The variable name can contain letters (uppercase and lowercase),


underscore( _ ) and digits only.
➔ The variable name must start with either letter, underscore or @
symbol.
➔ C# is case sensitive. It means age and Age refers to 2 different
variables.
➔ A variable name must not be a C# keyword. For example, if, for,
using can not be a variable name.

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

Console . WriteLine ( “ Practice Problem ” ) ; void args


} string Console
} WriteLine
}
19
How to declare variables in C#?

➔ Here's an example to declare a variable in C#.


◆ int age;
● In this example, a variable age of type int (integer) is
declared and it can only store integer values.
➔ We can assign a value to the variable later in our program like
such:
◆ int age;
◆ ... ... …
◆ age = 24;

20
How to declare variables in C#?

➔ However, the variable can also be initialized to some value during


declaration. For example,
◆ int age = 24;
● Here, a variable age of type int is declared and
initialized to 24 at the same time.
➔ Since, it’s a variable, we can change the value of variables as well.
For example,
◆ int age = 24;
◆ age = 35;
● Here, the value of age is changed to 35 from 24.
21
Data Types

➔ A data type, in programming, is a classification that specifies


which type of value a variable has and what type of mathematical,
relational or logical operations can be applied to it without causing
an error.
➔ A string, for example, is a data type that is used to classify text and
an integer is a data type used to classify whole numbers.

22
Data Types

There are two types of data type


➔ Built-in data types
◆ Fundamental data types (int, char, float, double, boolean,
short,void)
◆ Derived data types (array, string, structure)
➔ Programmer-defined data types (Union, Enumeration)

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;
}

char Used to denote a character 1 byte -128 to Example: ‘A’, ‘b’


type. A single character such 127 or 0
as a letter of the alphabet or to 255 {
punctuation. Character char Letter;
variables are letters of the Letter = ‘H’
alphabet, ASCII characters
or numbers 0-9. }
24
Data Type Description Storage Value range Example
size

float Used to denote a floating 4 bytes 1.2E-38 to Example: 2.54, -35.5


point type. It stores real or 3.4E+38 {
fractional numbers (also float weight;
called float to indicate a weight = 65.50;
floating point number). }

Array A finite sequence of variables of the same data type.

String An array of character variables. We can say , a collection of characters.

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 a same class and


display these variables.

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

You might also like