Day Dreaming

"A daydream is a visionary fantasy experienced while awake, especially one of happy, pleasant thoughts, hopes or ambitions."[Ref:Wikipedia]

Day 2: Chapter 10- Collections and Generics


Holla, back with next chapter this one is little vast so I cannot mention every detail..but I have tried to cover all the important points.So, Chapter 10 basically deals with Collections and generics in ASP.NET.
Collections – We have different types of collections and System.Collections has following Interfaces –
ICollection- Defines general characteristics (e.g., size, enumeration, thread safety) for all nongeneric collection types.
IComparer Allows two objects to be compared.
IDictionary Allows a nongeneric collection object to represent its contents using name/value pairs.
IDictionaryEnumerator- Enumerates the contents of a type supporting IDictionary.
IEnumerable- Returns the IEnumerator interface for a given object.
IEnumerator - Enables foreach style iteration of subtypes.
IHashCodeProvider - Returns the hash code for the implementing type using a customized hash algorithm.
IList - Provides behavior to add, remove, and index items in a list of objects. Also, this interface defines members to determine whether the implementing collection type is read-only and/or a fixed-size container.
Class Types of System.Collections –
ArrayList – Represents a dynamically sized array of objects
Hashtable - Represents a collection of objects identified by a numerical key Custom types stored in a Hashtable should always override System. Object.GetHashCode().
Queue - Represents a standard FIFO Queue.
SortedList - Like a dictionary; however, the Like a dictionary; however, the by ordinal position (e.g., index).
Stack - A last-in, first-out (LIFO) queue.I would use all these types in code.
Boxing and Unboxing – Boxing basically means converting a shorter(for e.g. Short) type in a wider type(for e.g. Object)..and unboxing is converting it back. Also, if you cast a value in wrong type you will get an invalidcastexception. To know more please visit here- http://www.codersource.net/csharp_boxing_unboxing.html
The problems without Generics –
1.    As a new object is allocated on managed heap and can lead to wastage of memory which in bigger application may be a problem.
2.    Type Safety – It is the property which operates on a particular type. You can create a type safe collection but if you need to use derived class objects we need to create another similar type collection or there will be a compile-time error and hence creating different methods for each and this can be a tedious task.
Generics Benefits –
1.    Generics provide better performance, as they do not result in boxing or unboxing penalties.
2.    Generics are more type safe, as they can only contain the “type of type” you specify.
3.    Generics greatly reduce the need to build custom collection types, as the base class library provides several prefabricated containers.
By convention, generic types specify their placeholders using common names. Although any letter (or word) will do, typically T is used to represent types, TKey is used for keys, and TValue is used for values.System.Collections.Generics has following Interfaces-
• ICollection
• IComparer
• IDictionary
• IEnumerable
• IEnumerator
• IList
We can create custom generic methods refer to swap method for example of it. We can also create interfaces which can have generics as parameters. For e.g. a interface which adds and subtracts numbers it can perform this for any numerical type using generics. Similarly we can have generic classes and structures.
The "default" keyword is overloaded in C# when used with generics; it represents the default value of a type parameter. Constraints are applied using the where keyword, the constraint list is placed after the generic type’s base class and interface list. e.g.
// MyGenericClass derives from MyBase and implements ISomeInterface,while the contained items must be structures.
public class MyGenericClass : MyBase, ISomeInterface where T : struct
We can also have base generic classes and interfaces, a derived class needs to specify a parameter type for T.To know more about Generics –
Code will be uploaded by early morning...:)

0 comments: