This document discusses various C# programming concepts including tokens, keywords, identifiers, literals, operators, punctuators, variables, parameters, boxing and unboxing, method overloading, and scopes. It defines tokens as the smallest elements in a program and describes the different types of tokens used in C#. It also explains the scopes of different types of variables and parameters as well as the concepts of boxing, unboxing, and method overloading.
Automating Google Workspace (GWS) & more with Apps Script
C# chap 4
1. Chapter 4
Literals, variables and Data Types
The smallest, non-reducible, textual elements in a program are referred to as Tokens. A C# program is a
collection of tokens, comments and white spaces. C# include the following five types of tokens:
Keywords, Identifiers, Literals, Operators, Punctuators
Keywords are an essential part of a language definition. They implement specific features of the
language. They are reserved, and cannot be used as identifiers except when they are prefaced by the @
character.
Identifiers are programmer –designed tokens. They are used for naming classes, methods, variables,
labels, namespace, interfaces, etc. C# identifiers enforce the following rules: They can have
alphabets, digits and underscore character. They must not begin with a digit. Upper case and lower case
letters are distinct.
Literals are the way in which the values that are stored in variables are represented.
Operators are symbols used in expressions to describe operations involving one or more operands.
Punctuators are symbols used for grouping and separating code. Punctuators in C# include:
Parentheses, Braces, Brackets, Semicolon, Colon, Comma, Period.
C# implements several types of statements:
Empty Statements, Labeled Statement, Declaration statements, Expression statement, Selection
statement, Interaction statements, Jump statements, try statements, checked statements, unchecked
statements, lock statements, using statements.
Scope of Variables
The scope of variables is the region of code within which the variable can be accessed.
C# variables are categorized into
- Static variables
- Instance Variables
- Array elements
- Value Parameters
- Reference Parameters
- Output Parameters
- Local variables
2. the scope of variables are
- m is a static variable
- n is an instance variable
- a is a value parameter
- b is an reference parameter
- c is an output parameter
- arr is an array element
-d is a local variable.
Static and instance variables are declared at the class level and are known as fields and ends when the
main method terminates. The reference and output parameters do not create new storage locations,
they represent the same storage locations as the variables that are passed as arguments.
The scope of a local variables starts immediately after its identifier in the declaration and extends up to
the end of the block containing the declaration. C# makes a fundamental distinction between the field
variables declared at the class level and the local variables declared within methods.
Boxing and Unboxing
Boxing means the conversion of a value type on the stack to a object type on the heap, conversely, the
conversion from an object type back to a value type is known as unboxing.
Boxing – Any type, value or reference can be assigned to an object without an explicit conversion.
When the compiler finds a value type where it needs a reference type, it creates an object ‘Box’ into
which it places the value of the value type.
Unboxing – Unboxing is the process of converting the object type back to the value type. A variable
that has previously been boxed can only be unboxed. While unboxing the object could be cast to any
type.
class Sample
{
static int m; int n;
void Test (int a, ref int b, out int
c, int [] arr)
{ int d = 100; …………
}
}
3. Method Overloading
- C# allows us to create more than one method with the same name, but with the different parameter
lists and different definitions. This is called method overloading. - Method overloading is used
when methods are required to perform similar tasks but using different input parameters.
- Overloaded methods must differ in number and/or type of parameters they take. This enables the
compiler to decide which one of the definitions to execute depending on the type and number of
arguments in the method call. - using the concept of method overloading, a family of methods
with one name but different arguments lists can be designed.
The method selection involves the following steps: 1) The compiler tries to find an exact match in
which the types of actual parameters are same and uses that method. 2) if the exact match
is not found, then the compiler tries to use the implicit conversions to the actual arguments and then
uses the method whose match is unique. If the conversion creates multiple matches, then the compiler
will generate an error message.