Showing posts with label type safety. Show all posts
Showing posts with label type safety. Show all posts

Friday, August 7, 2009

Chapter 13-Lesson 3

  • Platform Invoke (or P/Invoke) is sometimes required when we need to call Windows API or other unmanaged code which does not support .NET directly.
  • System.Runtime.InteropServices namespaces is required. System.Runtime.CompilerServices may also be required.
  • StringBuilder is preferrable over string during a P/Invoke.
  • All that needs to be done is to create a static external method with same name as the name of the function that needs to be called and decorate it with DllImport attribute, specifying the unmanaged dll. (This would only be a function declaration, similar to like we'd be writing prototype for that function)
  • After that whenever we call the function from within our managed code methods, the platform is invoked.
  • If we wish to convert data type from unmanaged to managed code we may us MarshalAs Attribute over a field or a parameter and specify any of the UnmanagedType enum values.
  • If an unmanaged function requires a callback, we may create a delegate specifying the signature that the callback method should possess, and while creating prototype for unmanaged code (one that requires a callback), give it an instance of that delegate. (See code on page 818)
  • This means that we cannot send a method name directly in the callback parameter of an unmanaged function due to Type-Safety.

Monday, June 15, 2009

Chapter 4-Lesson 5

  • Generic classes are type-safe compared to usual collections.
  • There exists a generic equivalent to almost all the collections discussed in earlier chapters. e.g. ArrayList as List<>, etc.
  • Generic List<> collection allows an overload of Sort( ) method to supply a generic delegate for comparison.
  • One difference between generic Dictionary<> class and its non-generic counterparts is that it does not use DictionaryEntry objects to hold items, instead it uses generic KeyValuePair.
  • Generic LinkedList<> is new in .NET 2.0. It consists of LinkedListNode objects and uses ILinkedListEnumerator.
  • Using foreach on a LinkedList will not return LinkedListNode type objects. Instead it will return objects of type that is used to store value in each LinkedList node. Explaination code on Page 255 of MS Press book.
  • Generic collections support generic interfaces, generic enumerators and generic comparisons for type-safety.
  • Three classes are common to almost all collections in System.Collections namespace (and any of its sub-namespaces), namely, CollectionBase, ReadOnlyCollectionBase and DictionaryBase.
  • From .NET 2.0 onwards, its preferrable to use generic collections than non-generic ones.