Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<Cookie> to CookieCollection in C#

Let's say I have List<Cookie> and I want to convert it to a CookieCollection. What's the easiest way to do this?

I know I can use a foreach loop, but isn't there a way to instantiate it with code similar to this?

List<Cookie> l = ...;
var c = new CookieCollection() { l };

When I try to compile that though, I get the error:

The best overloaded Add method 'System.Net.CookieCollection.Add(System.Net.CookieCollection)' for the collection initializer has some invalid arguments

btw, there are two Add methods that CookieCollection supports:

public void Add(Cookie cookie);
public void Add(CookieCollection cookies);
like image 957
Senseful Avatar asked Sep 01 '25 17:09

Senseful


1 Answers

Given c and l as in your first example, this'll do:

l.ForEach(c.Add);
like image 159
David Hedlund Avatar answered Sep 04 '25 05:09

David Hedlund