23
Collection A collection is a group of objects

Collection

Embed Size (px)

Citation preview

Page 1: Collection

Collection

A collection is a group of objects

Page 2: Collection

Collection

• A collection is a group of objects. The .NET Framework contains a large number of interfaces and classes that define and implement various types of collections.

• Originally, there were only non-generic collection classes.

Page 3: Collection

Collection• The principal benefit of collections is that they

standardize the way groups of objects are handled by your programs.

• All collections are designed around a set of cleanly defined interfaces. Several built-in implementations of these interfaces, such as ArrayList, Hashtable, Stack, and Queue, are provided, which you can use as-is.

• The .NET Framework supports five general types of collections: non-generic, specialized, bit-based, generic, and concurrent.

Page 4: Collection

non-generic• The non-generic collections have been part of the .NET

Framework since version 1.0. • They are defined in the System.Collections

namespace. • The non-generic collections are general-purpose data

structures that operate on object references. • Thus, they can manage any type of object, but not in a

type-safe manner.• you can mix various types of data within the same

collection.• non-generic collections do not have the type-safety that

is found in the generic collections.• The non-generic collections implement several

fundamental data structures, including a dynamic array, stack, and queue.

• They also include dictionaries, in which you can store key/value pairs.

Page 5: Collection

Specialized Collections

• The specialized collections operate on a specific type of data or operate in a unique way.

• For example, there are specialized collections for strings.

• The specialized collections are declared in System.Collections.Specialized.

• Specialized Collection:-HybridDictionary, ListDictionary, NameValueCollection, StringDictionary etc.

• System.Collections also defines three abstract base classes, CollectionBase, ReadOnlyCollectionBase, and DictionaryBase, which can be inherited and used.

Page 6: Collection

Bit-based

• The Collections API defines one bit-based collection called BitArray class.

• BitArray supports bitwise operations on bits, such as AND and XOR.

• BitArray is declared in System.Collections.• it still supports the basic collection underpinning

by implementing ICollection and IEnumerable.• You can construct a BitArray from an array of

Boolean values using this constructor:• public BitArray(bool[ ] values)

Page 7: Collection

Generic Collection• The generic collections provide generic implementations of several

standard data structures, such as linked lists, stacks, queues, and dictionaries.

• Because these collections are generic, they are type-safe. • This means that only items that are type-compatible with the type of

the collection can be stored in a generic collection, thus eliminating accidental type mismatches.

• Generic collections are declared in System.Collections.Generic namespace.

• There is a generic collection called LinkedList that implements a doubly linked list, but no non-generic equivalent.

• The generic collections work in the same way as the non-generic collections with the exception that a generic collection is type-safe.

• For all cases in which a collection is storing only one type of object, then a generic collection is your best choice

Page 8: Collection

Concurrent Collection

• The .NET Framework version 4.0 adds a new namespace called System.Collections.Concurrent.

• It contains collections that are thread-safe and designed to be used for parallel programming.

• The concurrent collections support multithreaded access to a collection.

• Concurrent Collection:-BlockingCollection<T>, ConcurrentDictionary<TKey, TValue> etc.

Page 9: Collection

Next…..

Page 10: Collection

Handling Network Errors

• To handle network exceptions that the program might generate, you must monitor calls to Create( ), GetResponse( ), and GetResponseStream( ).

• It is important to understand that the exceptions that can be generated depend upon the protocol being used.

• The following discussion describes several of the exceptions possible when using HTTP.

Page 11: Collection

Exceptions Generated by Create( )• The Create( ) method defined by WebRequest it can

generate four exceptions. • If the protocol specified by the URI prefix is not

supported, then NotSupportedException is thrown. • If the URI format is invalid, UriFormatException is

thrown. • If the user does not have the proper authorization, a

System.Security.SecurityException will be thrown.• Create( ) can also throw an ArgumentNullException if

it is called with a null reference, but this is not an error generated by networking.

• WebRequest.Create("http://www.McGraw-Hill.com")

Page 12: Collection

Exceptions Generated by GetReponse( )

• A number of errors can occur when obtaining an HTTP response by calling GetResponse( ).

• These are represented by the following exceptions: InvalidOperationException, ProtocolViolationException, NotSupportedException, and WebException. Of these, the one of most interest is WebException.

• WebException has two properties that relate to network errors: Response and Status.

• You can obtain a reference to the WebResponse object inside an exception handler through the Response property.

• When an error occurs, you can use the Status property of WebException to find out what went wrong.

Page 13: Collection

Exceptions Generated by GetResponseStream( )

• For the HTTP protocol, the GetResponseStream( ) method of WebResponse can throw a ProtocolViolationException, which, in general, means that some error occurred relative to the specified protocol.

• As it relates to GetResponseStream( ), it means that no valid response stream is available.

• An ObjectDisposedException will be thrown if the response has already been disposed.

• Of course, an IOException could occur while reading the stream, depending on how input is accomplished

Page 14: Collection

• The program begins by creating a WebRequest object that contains the desired URI. Notice that the Create( ) method, rather than a constructor, is used for this purpose. Create( ) is a static member of WebRequest. Even though WebRequest is an abstract class, it is still possible to call a static method of that class. Create( ) returns a WebRequest object that has the proper protocol “plugged in,” based on the protocol prefix of the URI. In this case, the protocol is HTTP. Thus, Create( ) returns an HttpWebRequest object. Of course, its return value must still be cast to HttpWebRequest when it is assigned to the HttpWebRequest reference called req. At this point, the request has been created, but not yet sent to the specified URI.

Page 15: Collection

• To send the request, the program calls GetResponse( ) on the WebRequest object. After the request has been sent, GetResponse( ) waits for a response. Once a response has been received, GetResponse( ) returns a WebResponse object that encapsulates the response. This object is assigned to resp. Since, in this case, the response uses the HTTP protocol, the result is cast to HttpWebResponse. Among other things, the response contains a stream that can be used to read data from the URI.

Page 16: Collection

• Next, an input stream is obtained by calling GetResponseStream( ) on resp. This is a standard Stream object, having all of the attributes and features of any other input stream. A reference to the stream is assigned to istrm. Using istrm, the data at the specified URI can be read in the same way that a file is read.

Page 17: Collection

Next….

Page 18: Collection

COM• COM has been Microsoft’s model for

programming components in a variety of languages, which can be built into distributed applications.

• The .NET Framework includes support for COM clients to use .NET components. When a COM client needs to create a .NET object, the CLR creates the managed object and a COM callable wrapper (CCW) that wraps the object. The COM client interacts with the managed object through the CCW.

Page 19: Collection

Types that need to be accessed by COM

clients must meet certain requirements:• The managed type (class, interface, struct, or

enum) must be public.• If the COM client needs to create the object, it

must have a public default constructor. COM does not support parameterized constructors.

• The members of the type that are being accessed must be public instance members.

• Private, protected, internal, and static members are not accessible to COM clients.

Page 20: Collection

Marshalling and Unmarshalling

• Marshalling governs how data is passed in method arguments and return values between managed and unmanaged memory during calls. Interoperation marshalling is a run-time activity performed by the common language runtime's marshaling service. In few words, "Marshalling" refers to the process of converting the data or the objects into a byte-stream, and "Unmarshalling" is the reverse process of converting the byte-stream back to their original data or object.

Page 21: Collection

Usefulness of marshalling

• The .NET framework is a natural progression from COM because the two models (assembly or com) share many central themes, including component reuse and language neutrality. For backward compatibility, COM interop provides access to existing COM components without requiring that the original component be modified. You can incorporate COM components into a .NET Framework application by using COM interop tools to import the relevant COM types. Once imported, the COM types are ready to use.

Page 22: Collection

COM

• COM interoperability also introduces forward compatibility by enabling your COM clients to access managed code as easily as they access other COM objects. Again, COM interoperability provides the means to seamlessly export metadata in an assembly to a type library and registers the managed component as a traditional COM component. Both the import and export utilities produce results consistent with COM specifications.

Page 23: Collection

• Dynamic Method Dispatch.

• Synchronous Calling.

• Method Group Conversion.