Day Dreaming

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

Chapter 10 : Collections and Generics - Code


Hi Everyone forgot to mention the types you should mention when specifying a constraint using where keyword the following table illustrates more -
Generic                                                                                    Constraint Meaning in Life
where T : struct                                     The type parameter must have System.ValueType in its chain of inheritance.
where T : class                                      The type parameter must not have System.ValueType in its chain of                                                             inheritance (e.g., must be a reference type).
where T : new()                                     The type parameter must have a default constructor. This is very helpful if your generic type must create an instance of the type parameter, as you cannot assume the format of custom constructors. Note that this constraint must be listed last on a multiconstrained type.
where T : NameOfBaseClass                   The type parameter must be derived from the class specified by NameOfBaseClass.
where T : NameOfInterface                     The type parameter must implement the interface specified by NameOfInterface.Multiple interfaces can be separated as a comma-delimited list.
Program Code- 

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...:)

Chapter 9: Program Code

Hi Everyone following are files you can add them to a new project and run your application.
The interface files really dont have any code...they are just most for the undertsanding. Cheers..will try
to post better examples..next time..I haven't implemented Callback Interfaces due to time constraint.

http://www.box.net/shared/rhb0tzqvye
http://www.box.net/shared/2hie3h9n03
http://www.box.net/shared/2b8syugy79
http://www.box.net/shared/odsav6l7rl
http://www.box.net/shared/khuzlafzov

DAY 1:CHAPTER 9 WORKING WITH INTERFACES

Ok Guyz..,First Post..So I am going to cover everything worth a mention in the Chapter and also attach a sample code with comments for understanding. Also as I mentioned the Book for reference APRESS PRO C#2008 .NET 3.5 Platform I am going to throw in my own ideas and contribute my knowledge and experience also…and I am assuming you have read first chapters before…
So, Chapter 9 basically deals with interfaces…and other important concepts include...Enumerable types, Cloneable Objects, Comparable objects and Callback interfaces.
Now, Interfaces can be declared using keyword interface…for e.g. interface IMyInterface . Use ‘I’ prefix as a conventions…You can add a new class from visual studio and create interface as there is no separate option to do so ...Here are few points worth mentioning –
One or more interfaces can be implemented by classes hence provide multiple inheritance indirectly. Also they help to isolate and unlike deriving classes (with abstract methods) you can choose which interface to implement.
By Default, it cannot have any variables or instance members can have only methods which are abstract and public and remember you will get an error if you try to make them public or abstract again explicitly.
It is illegal to – instantiate an interface object or have a constructor for interface.
Differences between Abstract Class and Interface-
1.    Abstract class may have implementation of methods but interface can have only declarations.
2.    Also Abstract class can have other members’ types but interfaces can have only abstract methods.
To determine if a type supports a specific interface is to make use of an explicit cast and then use  a try/catch or you could use the ‘as’ keyword or ‘is’ keyword. I would cover this with example code attached. Also Interface could be used as Parameters, Return types, also we can have arrays of interfaces.
Interfaces can be arranged into an interface hierarchy. Like a class hierarchy, when an interface extends an existing interface, it inherits the abstract members defined by the parent type(s).
Also if you implement two interfaces with a method with the same name you can use for e.g. We have IMyInterface1 and IMyInterface2 with method1(). You should implement using IMyinterface1.method1() to implement it. Refer to code example for implementation of these concepts.
Moving further, we have building enumerable types (IEnumerable and IEnumerator).Now we have foreach construct which can be used by classes which have implemented IEnumerable and  GetEnumerator() method. For e.g. System.Array can use it directly. But for custom classes and other classes we have following two ways-
1.    Implementing the IEnumerable interface and providing definition.(we have to use using System.Collections). Simply put, an iterator is a member that specifies how a container’s internal items should be returned when processed by foreach.
2.    You can just implement public IEnumerator GetEnumerator() method which iterates over the sub-items using internal foreach logic and returns each object to the caller using the yield return syntax.
Cloning – We can have deep or shallow copy of objects and we need to implement Icloneable  Interface and implement MemberwiseClone() and Clone() methods both return shallow copy of objects. To know more about cloning in .NET refer here - http://www.codeproject.com/KB/dotnet/Clone.aspx
Comparing Objects –
We have the IComparable interface with CompareTo() method which you can provide your own logic and use the Sort() method to sort the custom objects array. To Sort multiple objects we can use IComparer interface and provide implementation of Compare(obj1,obj2) which will then by using  Sort() method sort two different objects.
Callback Interfaces- 
Beyond using interfaces to establish polymorphism across diverse class hierarchies, namespaces,and assemblies, interfaces may also be used as a callback mechanism. This technique enables objects to engage in a two-way conversation using a common set of members.Callback interfaces are often not implemented by the object directly interested in receiving the events, but rather by a helper object called a sink object.
Unfortunately, I am still working on the code and the files will be uploaded by tomorrow morning…

Learning ASP.NET 25 days...

Hi everyone...I am nowadays trying to learn and understand ASP.NET. But I am having trouble finding motivation in working and really being passionate about it. It’s funny but at times parents especially moms keep scolding you and forcing you to work…and unfortunately I am not of the disciplined and really organized self-motivated people who would work voluntarily.
I don’t know why but I am sure there are a lot of you similar to me out there…you know just average people who look at great people like Steve Jobs…Bill Gates and other Pioneers…Geeks who have made it big time…I always wish I could be as passionate about something as them…unfortunately haven’t really found my passion..and I know I really need to work and not procrastinate…
So, I thought why not blog about whatever I study..and just keep my myself motivated to studying that way…I am reading the Apress Book Pro C# 2008 and .NET Platform 4th Edition. I have decided that I will be learning one chapter everyday…there are in all 33 chapters… but I would be posting from Part III- Advanced Programming Constructs… so from 9th chapter…so in all 25 days at that speed…I know it really will be tough at times…but if don’t do something like this I might end up not studying being ordinary…and I really want to have a life less ordinary and average at times…at the same time..I will be trying to put in my own experience and knowledge into it hence I really hope this will be helpful to other people and at the same time people can contribute their own ideas...
So, I will be posting one chapter notes everyday along with related code...everyday starting from 29th Jan…till 22nd of February…I will be having other things going on too..But that’s the challenge…and hopefully will do well…so enough talk and I will get to studying…as man behind Linux has said…

Talk is Cheap Show me Code – Linus Torvalds

Greatest Hits 2009

Holla everyone firstly wishing everyone a very happy new year!! This post is about me reflecting back at 2009 and remembering important events..
The year 2009 started for me back home...in India with my parents family and friends I had a great time. The last week I came back to US preponing my ticket in order not to miss the first lecture of Capstone project...
Hit # 1:I remember at that time all I could think of is getting a job...even when in India I had that back of my mind...finally I got a job as a Research Assistant...and it was one of the best things to happen so far...got half of my fees waived and also got resonable monthly income...so I felt great really...one of the greatest hits of the year..
As February began I concentrated and worked on my Capstone project and RA work..since I wanted the RA to continue in future also...We also had ISA elections and Holi..which was fun..and occupied my time with usual stuff..
Hit # 2 : We all roomies and had a blast at San Antonio- Six flags...the rides where awesome and it felt great hanging out and just chilling...We also visited river walk and couple museums..i think it was a break i needed and one of the times of the year where i had great fun..
In summer I continued with my RA work...and at the end of semester i came to know that RA would not be coninued...talked abt it more in my downslide..
Hit # 3: Was searching for a new job...and getting one in the labs...there were 25-30 people interview for the positions...I was the first person appointed..and it felt great to know that...dont know why feels great when you work hard for something and get it...
Hit # 4: Well this was something of a new experience…something little too personal too share for me here…I had highs and lows in this phase…but was a different experience…I was never so passionate and affected by anything else in my life before…I suffered a lot at times... don’t know if I took the right decisions in fact might have taken few wrong ones...but I was all good in this and it was not something that I could control… either but still learnt a lot with this...although not really a hit...this will be something I always remember...and I have recovered and it will help me in future…
Hit # 5: The final hit was graduating at the end of the year...something I wrote about last post…it feels good…and will be probably a nice memory to stay..
Finally…other than these I also did well in cricket…more about it here.
Winding up 2009…it was probably really a eventful year…I had a lot of different and great experiences…a lot of them for a first time…probably a year where I matured and became more responsible…but still my friends keep calling me ‘baccha’…but it was surely a year to remember with lot of milestones and hits...unfortunately the year my grandfather passed away…but the memories with him will always remain and be cherished by me…as will these greatest hits…

Beginning of a new year
with lot of hopes and aspirations
Not ruminate over past and think of this recession
will do my best to keep my new year resolutions..

In year 2009..made many mistakes..and learnt a lot of lessons..
but still had many joys and memories..with great deal of satisfaction
although at times had to suffer because of my own actions..
but that's how we learn and improve with realization..

In year 2010...will try to be better..
be a good son, a good friend, a good brother...
To see my parents smile..and family proud..
its more than enough a reason...for me
This year to work hard and be a better person..