Thursday, October 25, 2007

DOT NET 2.0 Notes Part 1

Q) What is a Reference Type?
A) Reference type store the address of their data, also known as pointer on the stack, actual data is stored on the Heap.

Q) What are partial Classes?
A) Partial classes allows you to split a class definition accross multiple source files.

Q) Why to use Generics?
A) Casting causes boxing & unboxing which steals processor time and slows performance. Also compiler cannot detect type errors when you cast to and from Object class, instead will throw an runtime exception.

Q)What is an Interface?
A)Interface also known as contract, define common set of members that all classes that implement must provide.

Q) What is an Event?
A) An event is a message sent by an object to signal the occurance of an action.The one that raises the event is called Event Sender and the one that receives is called Event receiver.

Q) What is a Delegate?
A) A delegate is a class that holds reference to a method. A delegate class has a signature and it can hold reference to those method that matches with its signature. It is similar to typesafe pointer or callback.A delegate declaration is sufficient to declare a delegate class.
E.g.
public delegate void ButtonClicked(object sender, EventArgs args);


Note: TryParse, TryParseExact and TryCast are new to NET 2.0 previously you had to attemp parsing or conversion and catch the exception.


Conversion in Custom Type:
Implement System.IConvertible to enable conversion through System.Convert.


FileSystemWatcher Class:
FileSystemWatcher fsw = new FileSystemWatcher(@"c:\test_env");
fsw.NotifyFilter = NotifyFilters.LastWrite;
//WaitForChanged is a Synchronous method for watching a directory
//for changes and for returning a structure that contains all changes.
WaitForChangedResult wcr = fsw.WaitForChanged(WatcherChangeTypes.All);
Console.WriteLine(wcr.Name);

There are events like Created, Changed, Renamed, and Deleted.

File can be opened for writing using following.
File.Open(@"c:\test.txt", FileMode.Create, FileAccess.Write);


Stream class is the abstract base class from which all other stream classes derive from.
FileStream, MemoryStream, CryptoStream, NetworkStream, GZipStream

Different ways to open file for reading or writing using stream classes.
StreamReader sr = File.OpenText(@"c:\test.txt");
StreamWriter sw = File.AppendText(@"c:\test.txt");
FileStream fw = File.OpenWrite(@"c:\test.txt");
FileStream fr = File.OpenRead(@"c:\test.txt");

//reads a stream as a string and not series of bytes.
StreamReader srFile = new StreamReader(@"c:\test.txt");
StreamWriter swFile = new StreamWriter(@"c:\test.txt");



StreamReader is derived from Abstract class TextReader and StreamWriter is derived from Abstract class TextWriter.

StringReader and StringWriter class reads or writes to inmemory strings.

BinaryReader and BinaryWriter:

Creating a binary file:
byte[] bt = new byte[]{95,96,97,98,99};
FileStream fbinw = File.Create(@"c:\temp\test.bin");
fbinw.Write(bt, 0, bt.Length);
fbinw.Flush();
fbinw.Close();

//using binarywriter
BinaryWriter brw = new BinaryWriter(fbinw);
brw.Write(bt);



MemoryStream:
Use StreamReader to read from MemoryStream and StreamWriter to write to MemoryStream.


ArrayList:
Add, AddRange, Insert, InsertRange Methods. Indexers to set item at specific position by overwriting the object at that position.
Remove, RemoveAt, RemoveRange
Clear to empty the collection
Supports IEnumerable interface, which allows to use foreach construct.
Implements interface ICollection derived from IEnumberable and IList derivied from ICollection.


Sequential Lists: Queue and Stack
Queue : FIFO
Stack : LIFO

Dictionary:
Map key to a Value, create a lookup tables.
To iterate using foreach you should cast the object to DictionaryEntry i.e. foreach(DictionaryEntry de in hstLookup).
All dictionary class Supports IDictionary Interface.
IDictionary Interface doesn't allow the access to item by index like IList, but only allows the access by key.
IDictionary Interface has Contains, ContainsKey and ContainsValue

Hashtable
SortedList: To create a list of items that can be sorted by key.

Hashtable is very efficient but has an overhead which for fewer than 10 items can impede performance, very useful for large collection.
ListDictionary: Implemented as simple array of items under the hood, useful for small collection.
HybridDictionary: If you do not know the size, internally it changes the listdictionary to hashtable as the list grows.
OrderdDictionary


BitArray:
No add or remove method, constructor takes the size and initializes all the items with false.
Xor Method of bitarray takes another bitarray object and result is another bitarray which is Xor of two bitarray.


StringCollection is Like an ArrayList except that it accepts only string, passing anything else with cause compilation error.
StringDictionary is like hashtable except that both key and value must be string.


using System.Collections.Specialized;
Hashtable ht = CollectionsUtil.CreateCaseInsensitiveHashtable();
ht.Add("a", "Apple");
ht["A"] = "APPLE";
Console.WriteLine("Count:{0}", ht.Count); // returns one

NameValueCollection appears like a StringDictionary class, but the Add method behaves differently.
The behaviour of Add and indexer is different, indexer will overwrite the previous value if the key supplied is same.

NameValueCollection nv = new NameValueCollection();
nv.Add("A", "apple");
nv.Add("A", "america");

foreach (string st in nv.GetValues("A"))
{
Console.WriteLine("Value for A : {0}", st);
}
Value for A : apple
Value for A : america



Questions:

Q) What type of collections can be made from CollectionsUtil class?
A) Case insensitive Hashtable and Case insensitive SortedList.

Q) What type of values can be stored in StringDictionary?
A) Strings.

No comments: