INTERFACES
* interfaces can also define events, patameterless properties, and parameterful properties(indexers in C#)
* CLR allows an interface to contain static methods, static fields, constants and static constructors but CLS doesnt allow
* C# prevents an interface from defining any static members.
* CLR doesnt allow an interface to contain any instance fields or instance constructors.
Definitions of 4 interfaces that are defined in the .NET Framework Class Library(FCL)
* IComparable
* Defines a generalized type-specific comparison method that a value type or class implements to order or sort its instances.
* The instance's IComparable implementation is called automatically by methods such as Array.Sort and ArrayList.Sort.
Public interface System.IComparable{
int32 CompareTo(Object object);
}
public class Temperature : IComparable
{
// The temperature value
protected double temperatureF;
public int CompareTo(object obj)
{
if (obj is Temperature)
{
Temperature otherTemperature = (Temperature)obj;
return this.temperatureF.CompareTo(otherTemperature.temperatureF);
}
else
{
throw new ArgumentException("Object is not a Temperature");
}
}
public double Fahrenheit
{
get
{
return this.temperatureF;
}
set
{
this.temperatureF = value;
}
}
public double Celsius
{
get
{
return (this.temperatureF - 32) * (5 / 9);
}
set
{
this.temperatureF = (value * 9 / 5) + 32;
}
}
}
public class CompareTemperatures
{
public static void Main()
{
ArrayList temperatures = new ArrayList();
// Initialize random number generator.
Random rnd = new Random();
// Generate 10 temperatures between 0 and 100 randomly.
for (int ctr = 1; ctr <= 10; ctr++)
{
int degrees = rnd.Next(0, 100);
Temperature temp = new Temperature();
temp.Fahrenheit = degrees;
temperatures.Add(temp);
}
// Sort ArrayList.
temperatures.Sort();
foreach (Temperature temp in temperatures)
Console.WriteLine(temp.Fahrenheit);
Console.Read();
}
}
* IEnumerable
* Exposes the enumerator, which supports a simple iteration over a non-generic collection.
public interface System.collections.IEnumerable{
IEnumerator GetEnumerator();
}
public class Person
{
public Person(string fName, string lName)
{
this.firstName = fName;
this.lastName = lName;
}
public string firstName;
public string lastName;
}
public class People : IEnumerable
{
private Person[] _people;
public People(Person[] pArray)
{
_people = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
{
_people[i] = pArray[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return new PeopleEnum(_people);
}
}
public class PeopleEnum : IEnumerator
{
public Person[] _people;
// Enumerators are positioned before the first element
// until the first MoveNext() call.
int position = -1;
public PeopleEnum(Person[] list)
{
_people = list;
}
public bool MoveNext()
{
position++;
return (position < _people.Length);
}
public void Reset()
{
position = -1;
}
public object Current
{
get
{
try
{
return _people[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
class App
{
static void Main()
{
Person[] peopleArray = new Person[3]
{
new Person("John", "Smith"),
new Person("Jim", "Johnson"),
new Person("Sue", "Rabon"),
};
People peopleList = new People(peopleArray);
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);
}
}
* This code produces output similar to the following:
*
* John Smith
* Jim Johnson
* Sue Rabon
*
*
* IEnumerator
* IEnumerator is the base interface for all nongeneric enumerators.
* The foreach statement of the C# language (for each in Visual Basic) hides the complexity of the enumerators
* Initially, the enumerator is positioned before the first element in the collection. The Reset method also brings the enumerator back to this position. At this position, calling the Current property throws an exception. Therefore, you must call the MoveNext method to advance the enumerator to the first element of the collection before reading the value of Current.
Public interface System.Collections.IEnumerator{
Boolean MoveNext();
void Reset();
Object Current{get;} //Read-only property
}
* ICollection
* Defines size, enumerators and synchronization methods for all collections.
* The ICollection interface is the base interface for classes in the System.Collections namespace.
* The ICollection interface extends IEnumerable;
* IDictionary and IList are more specialized interfaces that extend ICollection.
* An IDictionary implementation is a collection of key/value pairs, like the Hashtable class.
* An IList implementation is a collection of values and its members can be accessed by index, like the ArrayList class.
* Some collections that limit access to their elements, such as the Queue class and the Stack class, directly implement the ICollection interface.
* If neither the IDictionary interface nor the IList interface meet the requirements of the required collection, derive the new collection class from the ICollection interface instead for more flexibility.
Public interface System.Collections.ICollection : IEnumerable{
void CopyTo(Array, Int32 index); //When implemented by a class, copies the elements of the ICollection to an Array, starting at a particular Array index.
int32 Count {get;} //Read-only property. When implemented by a class, gets the number of elements contained in the ICollection.
Boolean IsSynchronized {get;} // Read only property. When implemented by a class, gets a value indicating whether access to the ICollection is synchronized (thread-safe).
Object SyncRoot {get;} // Read - only property. When implemented by a class, gets an object that can be used to synchronize access to the ICollection.
* ICloneable
public interface ICloneable{
object Clone()
}
An interface definition can be marked with modifiers - such as public, protechted, internal, and private the same way a class or structure is marked.
* Nonstatic members of an interface are always considered public and virtual.
* in C# if you implement an interface methoed in a type and omit the virtual keyword, the method is considered virtual and sealed - a type derived from the implementing type cant override the method.
* Like Reference type, a value type can implement zero, or more interfaces. However when you cast an instance of a value type to an interface type, the value type instance must be boxed. because they are ref types.
* IS-A - referes to specialization to generalization relationship. Car is four wheeler.
* if a derived type cant claim an is-a relationship with the base type, then dont use a base type;use an interface
* interfaces imply a CAN-DO relationship. if the CAN-DO functionality feels like it belons with various object types, use an interface.
IS-A-base class eg.
* System.IO.Stream class is an abstract base class. It Provides a bunch of methods, such as Read and Write.
* System.IO.FileStream, System.IO.MemoryStream and System.Net.Sockets.NetworkStream - are derived from Stream.
* Derived class need to implement only synchronous I/O operations; they innherit the ability to perform asynchronous I/O operations from the Stream base class.
* Button, CheckBoxm, ListBox are all derived from System.Windows.Forms.Control
CAN-DO interface Eg.
* FCL Collections are interface based.
* System.Collections namespace defines several collection related interfaces like IEnumerable, Icollection, IList, IDictionary.
* It has concrete classes like ArrayList, Hashtable, Queue, SortedList that implement combinations of above interfaces.
* Interface is used because the implementations of these various collection classes are radically different from one another.
* Or there isnt a lot of sharable code between an ArrayList, a Hashtable and a Queue.
* But they all maintain a set of elements that can be enumerated and allow addding and removing of elements and so interface is used.
* you have reference to an object whose type implements IList Interface. You can write code to add elements, remove elements, and search for an element without having to know what type of collection your are working with.
When you define an interface method in a type using an interface - qualified name, the method is considered private and cant be called usinga variable that is a refence to the type itself.
* to use the above interface method, you have to cast it to that interface. It cant have a public/private modifier.
Tuesday, January 27, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment