System.Net.CookieCollection Class

Assembly: System.dll
Namespace: System.Net
Summary
Provides a collection container for instances of the Cookie class.
C# Syntax:
[Serializable]
public class CookieCollection : ICollection, IEnumerable
Remarks
The CookieCollection class implements an ICollection interface to provide a general mechanism for handling collections of cookies. For example, this is useful in the case where an application is designed to store cookies for multiple servers.
See also:
System.Net Namespace | Cookie | CookieCollection | CookieContainer | CookieException | ICollection

System.Net.CookieCollection Member List:

Public Constructors
ctor #1 Default constructor. This constructor is called by derived class constructors to initialize state in this type.
Initializes a new instance of the CookieCollection class.
Public Properties
Count Read-only

Gets the number of cookies contained in a CookieCollection.
IsReadOnly Read-only

Gets a value indicating whether a CookieCollection is read-only.
IsSynchronized Read-only

Gets a value indicating whether access to a CookieCollection is thread safe.
Item Read-only

Overloaded:
Item[int index] {get

Gets the Cookie with a specific index from a CookieCollection.
Item Read-only

Overloaded:
Item[string name] {get

Gets the Cookie with a specific name from a CookieCollection.
SyncRoot Read-only

Gets an object that you can use to synchronize access to the CookieCollection.
Public Methods
Add Overloaded:
Add(Cookie cookie)

Adds a Cookie to a CookieCollection.
Add Overloaded:
Add(CookieCollection cookies)

Adds the contents of a CookieCollection to the current instance.
CopyTo Copies the elements of a CookieCollection to an instance of the Array class, starting at a particular index.
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals

Derived from System.Object, the primary base class for all objects.
GetEnumerator Gets an enumerator that can iterate through a CookieCollection.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode

Derived from System.Object, the primary base class for all objects.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType

Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Object)
See base class member description: System.Object.ToString

Derived from System.Object, the primary base class for all objects.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize

Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone

Derived from System.Object, the primary base class for all objects.

Hierarchy:


System.Net.CookieCollection Member Details

ctor #1
Summary
Initializes a new instance of the CookieCollection class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public CookieCollection();
Remarks
The following table shows initial property values for an instance of CookieCollection.

Property Default
IsReadOnly true
Count 0
IsSynchronized false
See also:
Cookie | CookieCollection | CookieContainer | CookieException

Return to top


Property: Count (read-only)
Summary
Gets the number of cookies contained in a CookieCollection.
C# Syntax:
public int Count {get;}
Implements:
ICollection.Count
See also:
Cookie | CookieCollection | CookieContainer | CookieException

Return to top


Property: IsReadOnly (read-only)
Summary
Gets a value indicating whether a CookieCollection is read-only.
C# Syntax:
public bool IsReadOnly {get;}
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: IsSynchronized (read-only)
Summary
Gets a value indicating whether access to a CookieCollection is thread safe.
C# Syntax:
public bool IsSynchronized {get;}
Implements:
ICollection.IsSynchronized

Return to top


Overloaded Property: Item (read-only)
Summary
Gets the Cookie with a specific index from a CookieCollection.
C# Syntax:
public Cookie this[int index] {get;}
Parameters:

index

The zero-based index of the Cookie to be found.

Exceptions
Exception Type Condition
ArgumentOutOfRangeException index < 0 or index >= Count
Remarks
You can use this to iterate over the contents of a CookieCollection.
Example
		// Get the cookies in the 'CookieCollection' object using the 'Item' property.
		// The 'Item' property in C# is implemented through Indexers. 
      // The class that implements indexers is usually a collection of other objects. 
      // This class provides access to those objects with the '<class-instance>[i]' syntax. 
		try {
			if(cookies.Count == 0) {
				Console.WriteLine("No cookies to display");
				return;
			}
			for(int j = 0; j < cookies.Count; j++)
				Console.WriteLine("{0}", cookies[j].ToString());
			Console.WriteLine("");
		}
		catch(Exception e) {
			Console.WriteLine("Exception raised.\nError : " + e.Message);
		}

    
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Overloaded Property: Item (read-only)
Summary
Gets the Cookie with a specific name from a CookieCollection.
C# Syntax:
public Cookie this[string name] {get;}
Parameters:

name

The name of the Cookie to be found.

Exceptions
Exception Type Condition
ArgumentNullException name is null.
Remarks
You can use this to iterate over the contents of a CookieCollection.
Example
		// Get the cookies in the 'CookieCollection' object using the 'Item' property.
		// The 'Item' property in C# is implemented through Indexers. 
		// The class that implements indexers is usually a collection of other objects. 
		// This class provides access to those objects with the '<class-instance>[i]' syntax. 
		try {
			if(cookies.Count == 0) {
				Console.WriteLine("No cookies to display");
				return;
			}
			Console.WriteLine("{0}", cookies["UserName"].ToString());
			Console.WriteLine("{0}", cookies["DateOfBirth"].ToString());
			Console.WriteLine("{0}", cookies["PlaceOfBirth"].ToString());
			Console.WriteLine("");
		}
		catch(Exception e) {
			Console.WriteLine("Exception raised.\nError : " + e.Message);
		}

    
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Property: SyncRoot (read-only)
Summary
Gets an object that you can use to synchronize access to the CookieCollection.
C# Syntax:
public object SyncRoot {get;}
Implements:
ICollection.SyncRoot
Remarks
The CookieCollection.SyncRoot property returns an object that can be used to synchronize access to the CookieCollection.
See also:
ICollection

Return to top


Overloaded Method: Add(
   Cookie cookie
)
Summary
Adds a Cookie to a CookieCollection.
C# Syntax:
public void Add(
   Cookie cookie
);
Parameters:

cookie

The Cookie to be added to a CookieCollection

Exceptions
Exception Type Condition
ArgumentNullException cookie is null
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Overloaded Method: Add(
   CookieCollection cookies
)
Summary
Adds the contents of a CookieCollection to the current instance.
C# Syntax:
public void Add(
   CookieCollection cookies
);
Parameters:

cookies

The CookieCollection to be added .

Exceptions
Exception Type Condition
ArgumentNullException cookies is null
Remarks
Each Cookie is read from the CookieCollectioncookies and added to the current instance.
See also:
CookieCollection | CookieContainer | CookieException

Return to top


Method: CopyTo(
   Array array,
   int index
)
Summary
Copies the elements of a CookieCollection to an instance of the Array class, starting at a particular index.
C# Syntax:
public void CopyTo(
   Array array,
   int index
);
Parameters:

array

The target Array to which the CookieCollection will be copied.

index

The zero-based index in the target Array where copying begins.

Implements:
ICollection.CopyTo
Remarks
The Arrayarray must be one-dimensional with zero-based indexing.

Return to top


Method: Equals(
   object obj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
   object obj
);

For more information on members inherited from System.Object click on the link above.

Return to top


Method: Finalize()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~CookieCollection();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetEnumerator()
Summary
Gets an enumerator that can iterate through a CookieCollection.
C# Syntax:
public IEnumerator GetEnumerator();
Return Value:
An instance of an implemention of an IEnumerator interface that can iterate through a CookieCollection.
Implements:
IEnumerable.GetEnumerator
Remarks
You should use an IEnumerator only to read data in the collection. Enumerators cannot be used to modify the underlying collection. The enumerator does not have exclusive access to the collection.

When an enumerator is created, it takes a snapshot of the current state of the collection. If changes such as adding, modifying, or deleting elements are made to the collection, the snapshot gets out of sync and the enumerator throws an InvalidOperationException. Two enumerators created from the same collection at the same time can produce different snapshots of the collection.

Return to top


Method: GetHashCode()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: GetType()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: MemberwiseClone()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top


Method: ToString()
Inherited
See base class member description: System.Object.ToString
C# Syntax:
public virtual string ToString();

For more information on members inherited from System.Object click on the link above.

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.