WHAT IS TYPE?
In daily usage, the word type usually signifies a number of persons or items sharing one or more particular characteristics. This often causes a group of people of the same type to be regarded as a more or less precisely defined group.In C#, all values belonging to a specific type can be used to share a set of predefined characteristics.
THE TYPES OF C#
C# is a strongly typed language.Every value in C# must have a type.There are two kinds of data types in C#.
- Value Types(implicit data types, structs and enumeration)
- Reference Types(objects, delegates)
Value types are passed to methods by passing an exact copy while Reference types are passed to methods by passing only their reference (handle). Implicit data types are defined in the language core by the language vendor, while explicit data types are types that are made by using or composing implicit data types.
Implicit data types in .Net compliant languages are mapped to types in the Common Type System (CTS) and CLS (Common Language Specification). Hence, each implicit data type in C# has its corresponding .Net type. The implicit data types in C# are:
Implicit data types are represented in language using keywords, so each of the above is a keyword in C# (Keyword are the words defined by the language and can not be used as identifiers). It is worth noting that string is also an implicit data type in C#, so string is a keyword in C#. The last point about implicit data types is that they are value types and thus stored on the stack, while user defined types or referenced types are stored using the heap.
A stack is a data structure that store items in a first in first out (FIFO) fashion. It is an area of memory supported by the processor and its size is determined at the compile time. A heap consists of memory available to the program at run time. Reference types are allocated using memory available from the heap dynamically (during the execution of program). The garbage collector searches for non-referenced data in heap during the execution of program and returns that space to Operating System.
VARIABLES
During the execution of a program, data is temporarily stored in memory. A variable is the name given to a memory location holding a particular type of data. So, each variable has associated with it a data type and a value. In C#, variables are declared as:
Ex : int i;
The above line will reserve an area of 4 bytes in memory to store an integer type values, which will be referred to in the rest of program by the identifier 'i'. One can initialize the variable as declared (on the fly) and can also declare/initialize multiple variables of the same type in a single statement,
Ex : int i = 2 ;
In C# (like other modern languages), the variables must be declared before being used. Also, there is the concept of "Definite Assignment" in C# which says "local variables (variables defined in a method) must be initialized before being used".
Constant Variables or Symbols
Constants are variables whose values, once defined, can not be changed by the program. Constant variables are declared using the const keyword, like:
const double PI = 3.142;
Constant variables must be initialized as they are declared. It is a syntax error to write:
const int MARKS;
It is conventional to use capital letters when naming constant variables.
No comments:
Post a Comment