Page 36 - DCAP312_WEB_TECHNOLOGIES_II
P. 36
Web Technologies-II
Notes Console.WriteLine (“Good morning, {0}”, name);
Each variable is declared with an explicit type. Only values whose types are compatible with
the variable’s declared type can be bound to (stored in) the variable.
C# supports several program elements corresponding to the general programming concept of
variable: fields, parameters, and local variables.
Fields
Fields, from time to time called class-level variables, are variables linked with classes or structures.
An instance variable is a field connected with an instance of the class or structure, while a static
variable, declared with the static keyword, is a field associated with the type itself. Fields can
also be associated with their class by making them constants (const), which requires a declaration
assignment of a constant value and prevents subsequent changes to the field.
Each field has a visibility of public, protected, internal, and protected internal, or private (from
most visible to least visible).
Local Variables
Like fields, local variables can optionally be constant (const). Constant local variables are stored
in the assembly data region, while non-constant local variables are stored (or referenced from)
the stack. They thus have both a scope and an extent of the method or statement block that
declares them.
Parameters
Parameters are variables connected with a method. An in restriction may either have its value
passed in from the callee to the method’s environment, so that changes to the parameter by the
method do not affect the value of the callee’s variable, or passed in by reference, so that changes
to the variables will affect the value of the callee’s variable. Value types (int, double, string) are
passed in “by value” while reference types (objects) are passed in “by reference.”
An out parameter does not have its value copied, thus changes to the variable’s value within the
method’s environment directly affect the value from the callee’s environment. Such a variable is
considered by the compiler to be unbound upon method entry, thus it is illegal to reference an
out parameter before assigning it a value. It also must be assigned by the method in each valid
(non-exceptional) code path through the method in order for the method to compile.
A reference parameter is similar to an out parameter, except that it is bound before the method
call and it need not be assigned by the method. A params parameter represents a variable
number of parameters.
If a method signature includes one, the params argument must be the last
argument in the signature.
2.1.3 Types
Each type in C# is either a value type or a situation type. C# has several predefined (“built-in”)
types and allows for statement of custom value types and reference types.
Integral Types
Because the type system in C# is unified with other languages that are CLI-compliant, each
integral C# type is actually an alias for a corresponding type in the .NET framework. Although the
names of the aliases vary between .NET languages, the underlying types in the .NET framework
remain the same. Thus, objects created in assemblies written in other languages of the .NET
Framework can be bound to C# variables of any type to which the value can be converted, per
30 LOVELY PROFESSIONAL UNIVERSITY