Convert <Cookie> to CookieCollection in C #

Say I have List<Cookie>one and I want to convert it to CookieCollection. What is the easiest way to do this?

I know I can use the foreach loop, but there is no way to create it with code like this?

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

When I try to compile this, I get an error message:

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

btw, there are two methods Addthat supports CookieCollection:

public void Add(Cookie cookie);
public void Add(CookieCollection cookies);
+3
source share
4 answers

CookieCollection .Net 2 ( Generics). , , foreach.

+4

c l, , :

l.ForEach(c.Add);
+8

You can pass the lambda to the ForEach method from the list. This will work regardless of the CookieCollection constructors.

List<Cookie> l = ...;
var c = new CookieCollection();
l.ForEach(tempCookie => c.Add(tempCookie));
+1
source
List<Cookie> l = ...;
var c = new CookieCollection();
l.ForEach(x => c.Add(x));
0
source

Source: https://habr.com/ru/post/1727428/


All Articles