Professional Documents
Culture Documents
Important Definitions:
1. Class: A C# program is a collection of classes. A class is a set of declaration statements and
methods. A method has executable statements.
2. Token: The smallest textual element that cannot be reduced further is called a token. There are 5
types of tokens in C#. These are:
i) Keywords
ii) Identifiers
iii) Literals
iv) Operators
v) Punctuation symbols
White spaces (space, tab and newline are spaces) and comments are not treated as tokens.
3. Keywords: Keywords are reserved words that have a specific meaning in a programming language.
These keywords cannot be used as identifiers or variables. C# permits keywords to be used as
identifiers provided the keyword is preceded by the @ symbol. However, this must be avoided.
4. Identifiers: Identifiers are created by the programmer and are used to give names to classes,
methods, variables, namespaces, etc.
5. Literals: Literals are constant assigned to variables. Examples of literals are: 250, ‘A’, “Mumbai”.
6. Operators: Operators are symbols used in expressions. An operator acts on operands. Some
operators like ‘+’ and ‘*’ require two operands (e.g. 23 + 67), but some operators are unary
operators because they require only one operand (e.g., the unary minus operator, -23).
7. Punctuation Symbols: Symbols such as brackets, semicolon, colon, comma, period, etc are called
punctuation symbols or punctuators or separators. They are used for grouping statements
together, or for terminating a statement, etc.
8. Statements: A statement is an executable combination of tokens. A statement in C# ends with the
semicolon symbol (;). Various types of statements in C# are:
i) Declaration statements ii) Expression statements iii) Selection statements
iv) Jump statements v) Interaction statements vi) labeled statements
vii) Empty statements
Page 1 of 6
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
Keywords :
C# has 76 keywords. A few commonly used keywords are listed below:
Literals:
Literals are value constants assigned to variables. The various types of literals in C# are as follows:
1. Numeric Literals
a. Integer Literals -- e.g., 250, 200, -187, 0, 0xcab (hexadecimal literal)
b. Real Literals -- e.g., 3.142, -96.4, 0.0074, 6.023E+3
2. Boolean Literals -- e.g., true, false
3. Character Literals
a. Single character literals -- e.g., ‘A’, ‘4’ , ‘?’, ‘ ‘
b. String literals -- e.g., “Welcome 2009”, “Hello”, “9869012345”
c. Backslash Character Literals -- These are the escape sequences. They are used for
formatting the printed output. The most commonly used escape sequences are:
i. ‘\n’ -- new line character
ii. ‘\b’ -- back space
iii. ‘\f’ -- form feed
iv. ’\t’ -- horizontal tab
v. ’\v’ -- vertical tab
Variables:
Page 2 of 6
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
2. Programming in C#
4. The value of a variable can change during the execution of a program.
5. Every variable has a data type. E.g., an int variable can only store integer type data, etc.
6. The place at which a variable is declared determines the scope of that variable in the program.
7. All variable must be declared before they are used in a program.
8. The data type of a variable determines the following:
a. The space occupied in the memory by the variable. E.g., in C#, int occupies 4 bytes.
b. The operations that can be carried out on that variable. E.g., we can carry out the operation
of % (modulus) on integer operands but not on char operands.
c. The range of values that can be stored in the variable.
Here are some examples of valid and invalid variable names. For each keyword, write “Valid” or
“Invalid”. If it is invalid, justify.
Data Types:
C# supports two types of data types. These are:
1. Value types, and
2. Reference Types.
1. Value types :-
i) These are of fixed length.
ii) They are stored on a stack.
iii) When a variable is assigned to another variable, the values are actually copied.
Page 3 of 6
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
2. Reference types :-
i) These are of variable length.
ii) They are stored on a heap.
iii) When an assignment occurs between reference variables, only the reference is copied. The
actual value remains the same in memory. There are now two references to the same memory
location.
The object type is the base type for all other inbuilt and user-defined types in C#. The string type is
used for creating and performing many operations on string type data.
Page 4 of 6
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
2. Programming in C#
Constant Variables
Variables whose values do not change during the execution of the program are called constants.
E.g., suppose the programmer declares a variable called PI which should not change during the
program execution. We declare this variable as follows:
const PI = 3.142;
Note:
i) const must be declared at class level. They cannot be declared at method level.
ii) Once a constant has been assigned a value, it should not be assigned another value.
iii) In C#, constants are assigned for data types.
Scope of Variables:
C# is a block-structured language. The lifetime of a variable and its accessibility is known as the scope
of the variable. A variable has a scope or visibility within a particular block. That is, the variable can
be accessed within that block. The scope of the variable depends upon the place of declaration and type
of variable.
Page 5 of 6
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
using System;
namespace localscope
{
class Scope
{
public static void Main()
{
for (int x = 1; x <= 10; x++)
{
Console.WriteLine("x is {0}", x);
}
Console.WriteLine("Outside the loop, x is {0}", x);
}
}
}
The variable x is declared as part of the “for” statement. As soon as the “for” statement is completed,
the variable x goes out of scope. Since it is out of the scope, the second WriteLine statement generates
an error.
IMPORTANT QUESTIONS
Page 6 of 6