Programs, objects and methods
.NET is essentially a set of tools to build and execute computer programs.
A program is a set of instructions given to a computer that execute those instuctions automatically to manipulate some data.
.NET follows the object-oriented paradigm which means that the instructions and data are grouped around ‘’objects’’ that represent things or concepts. Objects that share the same instructions and manipulate the same kind of data are grouped into the same type.
Types are a way to classify the concepts or objects of a language.
System types: Types that are part of the .NET framework class library is called system types. Custom types: Types that will be constructed by the developer is called custom types.
eg:Writing object oriented programs can be seen as the process of defining one or more custom types.
Namespaces are a way to organize types so that they can be more easily found.
In the context of namespaces the System types are the types included in the System namespace or one of its sub-namespace .
Custom types (non system types) should use other namespaces.
There are other ways to categorize types in .NET. One of them is by the way the objects created based on those types are mapped to the computer memory. This will give us Value types and Reference types.
Value types
An instance of a value type directly contains its data (value). stored in stack.
The value types are themselves split in 3 categories:
- The built-in value types -
- the integer types (Byte, SByte, Int16, Int32, Int64, UInt16, UInt32 and UInt64)
- the floating point types (Single and Double)
- the logical type (Boolean)
- other (Char, Decimal, InPtr and UInPtr)
2. User-defined value types
3. Enumerations
Value types cannot equal null.
int a=null; will throw an error !!
Reference Type
Contrary to value types, an instance of a reference type does not directly contain its data (value), but instead contains some kind of a reference to the memory location of that value.
For example a String local variable has memory allocated on the stack for a reference to the contained string, not the string itself. In that case the string itself will be allocated on the heap and garbage collected.
Unlike Value types, reference types can be assigned the value null.
The reference types are themselves split in four categories:
- pointers
- arrays
- classes
- interfaces
An array is essentially a group of objects, usually of the same type, that can be accessed via an index.
The two main problems of arrays are:
* They are fixed length
* They support only one internal organization
Classes
Classes are themselves split in three:
* user-defined classes
* boxed value types
* delegates
Boxing and Unboxing
All types derive diretly or indirectly from System.Object (including value types by the way of System.ValueType).
Boxing and unboxing enable value types to be treated as objects.
Boxing a value type, packages it, inside an instance of the Object reference type. This allows the value type to be stored on the garbage collected heap.
------------------------------------------------------------------------------------------
int i = 123;
object o = (object) i; // boxing
-----------------------------------------------------------------------------------------
Please also note that it is not necessary to explicitly cast an integer to an object (as shown in the example above) to cause the integer to be boxed. Invoking any of its methods would also cause it to be boxed on the heap (because only the boxed form of the object has a pointer to a virtual method table):
--------------------------------------------------------------------------------------------
int i=123;
String s=i.toString(); //This call will cause boxing
---------------------------------------------------------------------------------------------
There is also a third way in which a value type can be boxed. That happens when you pass a value type as a parameter to a function that expects an object. Let's say there is a function prototyped as:
void aFunction(object value)
Now let's say from some other part of your program you call this function like this:
int i=123;
aFunction(i); //i is automatically boxed
This call would automatically cast the integer to an object, thus resulting in boxing.
------------------------------------------------------------------------------------------
The object o can then be unboxed and assigned to integer variable i:
------------------------------------------------------------------------------------------
o = 123;
i = (int) o; // unboxing
------------------------------------------------------------------------------------------
Performance of boxing and unboxing
In relation to simple assignments, boxing and unboxing are computationally expensive processes.
When a value type is boxed, an entirely new object must be allocated and constructed. To a lesser degree, the cast required for unboxing is also expensive computationally.
Unboxing extracts the value type from the object
Interfaces
It is a form of polymorphism . to have a single type of reference to the "common part" of different types that have "something in common".
The class libraries rely heavily on interfaces and a clear understanding of the concept is essential.
An interesting problem with interfaces compared with inheritance is that you do not get a default implementation (virtual parent methods) so you have to recode everything. The techniques and tools do exist here but there is always some extra work to do. More on that with generics...
Another way of type is by Reflection category (Class, Value types, Interfaces, Generics, etc.).
Yet another way is to distinguish the types that are directly supported by the runtime (built-in types) from those defined either in the class libraries or custom.
Those categories can also be intersected with one another, that will give us such things as "Built-in value types" or "System interfaces".
Data structures
------------------
Stack
A stack in an ordered list where insertion and deletion take place at one end called top of the stack (stack pointer) .So the last inserted item will be removed first. Data accessing in the order of Last in First Out Order[LIFO].
Queue,dequeue,Priority Queue
With a queue, you insert items at one and and extract them from the
other end -You can think of it as a line in a grocery store. The first one in the line is the first one to be served.Just like a queue.
i.e. you can ONLY get things out in the order in which they were inserted.FIFO (First In First Out)
With a deque, you can insert and/or remove from either end, so you get a
combination of stack behavior (remove the most recently inserted item)
or queue behavior (as above).
A priority queue is different from either of these. With a priority
queue, you specify an ordering by which items will be sorted. You
insert items and you remove items, but when you remove items, you get
them in sorted order. This is handy for things like tasks that have to
be completed. When a task arises that will need to be done, you put it
into the priority queue. When you're ready to carry out a task, you
pull one from the priority queue, and you'll get the highest priority
task that's waiting.
Linked List
A linked list is a data structure that consists of a sequence of data records such that in each record there is a field that contains a reference (i.e., a link) to the next record in the sequence.
Each record of a linked list is often called an element or node.
The field of each node that contains address of the next node is usually called the next link or next pointer. The remaining fields may be called the data, information, value, or payload fields.
The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant number of operations.
Linear and circular lists
In the last node of a list, the link field often contains a null reference, a special value that is interpreted by programs as meaning "there is no such node". A less common convention is to make it point to the first node of the list; in that case the list is said to be circular or circularly linked; otherwise it is said to be open or linea
Singly-linked lists contain nodes which have a data field as well as a next field, which points to the next node in the linked list
In a doubly-linked list, each node contains, besides the next-node link, a second link field pointing to the previous node in the sequence. The two links may be called forward(s) and backwards.
Attributes
First of all attributes are not types. They are elements of information that are added to a program element (assemblies, classes, method, etc.) to qualify it outside of the normal code associated with that element.
System attributes (part of the framework) and Custom attributes (built by the developer).
eg:get,set
Collections
Collection is a group of objects.
We have two sections with collections examples:
Generics
Generic programming or the use of parameterized types is not an object oriented concept.
The actual type will be obtained when you substitute the parameter type with a specific type (For example when you declare a variable of that type).
// C#
List myIntList = new List()
'// VB.NET
Dim myIntList As New List(Of Integer)
Delegates
A delegate is a type that defines a reference to a single function that
must have the same signature as the delegate definition. A signature is
a description of the type of the function parameters and its return
type. Like a class you can create an object of that type. The object
created is a reference to a function that can be assigned (set the
reference to a specific function), be passed as parameter or executed
(the function that is referenced is actually executed). Unlike an
interface, a delegate defines only one function and a delegate instance
can be created directly (no need to have another class implementing the
interface).