Day Dreaming

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

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…

0 comments: