11using System ;
22using System . Collections . Generic ;
3- using System . Text ;
43using System . Collections . Specialized ;
4+ using System . Text ;
55
66#if SILVERLIGHT
77using Hammock . Silverlight . Compat ;
1111
1212namespace RestSharp . Authenticators . OAuth . Extensions
1313{
14- internal static class CollectionExtensions
15- {
16- public static IEnumerable < T > AsEnumerable < T > ( this T item )
17- {
18- return new [ ] { item } ;
19- }
20-
21- public static IEnumerable < T > And < T > ( this T item , T other )
22- {
23- return new [ ] { item , other } ;
24- }
25-
26- public static IEnumerable < T > And < T > ( this IEnumerable < T > items , T item )
27- {
28- foreach ( var i in items )
29- {
30- yield return i ;
31- }
32-
33- yield return item ;
34- }
35-
36- public static K TryWithKey < T , K > ( this IDictionary < T , K > dictionary , T key )
37- {
38- return dictionary . ContainsKey ( key ) ? dictionary [ key ] : default ( K ) ;
39- }
40-
41- public static IEnumerable < T > ToEnumerable < T > ( this object [ ] items ) where T : class
42- {
43- foreach ( var item in items )
44- {
45- var record = item as T ;
46- yield return record ;
47- }
48- }
49-
50- public static void ForEach < T > ( this IEnumerable < T > items , Action < T > action )
51- {
52- foreach ( var item in items )
53- {
54- action ( item ) ;
55- }
56- }
14+ internal static class CollectionExtensions
15+ {
16+ public static IEnumerable < T > AsEnumerable < T > ( this T item )
17+ {
18+ return new [ ] { item } ;
19+ }
20+
21+ public static IEnumerable < T > And < T > ( this T item , T other )
22+ {
23+ return new [ ] { item , other } ;
24+ }
25+
26+ public static IEnumerable < T > And < T > ( this IEnumerable < T > items , T item )
27+ {
28+ foreach ( var i in items )
29+ {
30+ yield return i ;
31+ }
32+
33+ yield return item ;
34+ }
35+
36+ public static K TryWithKey < T , K > ( this IDictionary < T , K > dictionary , T key )
37+ {
38+ return dictionary . ContainsKey ( key ) ? dictionary [ key ] : default ( K ) ;
39+ }
40+
41+ public static IEnumerable < T > ToEnumerable < T > ( this object [ ] items ) where T : class
42+ {
43+ foreach ( var item in items )
44+ {
45+ var record = item as T ;
46+ yield return record ;
47+ }
48+ }
49+
50+ public static void ForEach < T > ( this IEnumerable < T > items , Action < T > action )
51+ {
52+ foreach ( var item in items )
53+ {
54+ action ( item ) ;
55+ }
56+ }
5757
5858#if ! WINDOWS_PHONE
5959
60- public static void AddRange ( this IDictionary < string , string > collection , NameValueCollection range )
61- {
62- foreach ( string key in range . AllKeys )
63- {
64- collection . Add ( key , range [ key ] ) ;
65- }
66- }
67-
68- public static string ToQueryString ( this NameValueCollection collection )
69- {
70- var sb = new StringBuilder ( ) ;
71- if ( collection . Count > 0 )
72- {
73- sb . Append ( "?" ) ;
74- }
75-
76- var count = 0 ;
77- foreach ( var key in collection . AllKeys )
78- {
79- sb . AppendFormat ( "{0}={1}" , key , collection [ key ] . UrlEncode ( ) ) ;
80- count ++ ;
81-
82- if ( count >= collection . Count )
83- {
84- continue ;
85- }
86- sb . Append ( "&" ) ;
87- }
88- return sb . ToString ( ) ;
89- }
60+ public static void AddRange ( this IDictionary < string , string > collection , NameValueCollection range )
61+ {
62+ foreach ( var key in range . AllKeys )
63+ {
64+ collection . Add ( key , range [ key ] ) ;
65+ }
66+ }
67+
68+ public static string ToQueryString ( this NameValueCollection collection )
69+ {
70+ var sb = new StringBuilder ( ) ;
71+ if ( collection . Count > 0 )
72+ {
73+ sb . Append ( "?" ) ;
74+ }
75+
76+ var count = 0 ;
77+ foreach ( var key in collection . AllKeys )
78+ {
79+ sb . AppendFormat ( "{0}={1}" , key , collection [ key ] . UrlEncode ( ) ) ;
80+ count ++ ;
81+
82+ if ( count >= collection . Count )
83+ {
84+ continue ;
85+ }
86+ sb . Append ( "&" ) ;
87+ }
88+ return sb . ToString ( ) ;
89+ }
9090
9191#endif
9292
93- public static string Concatenate ( this WebParameterCollection collection , string separator , string spacer )
94- {
95- var sb = new StringBuilder ( ) ;
96-
97- var total = collection . Count ;
98- var count = 0 ;
99-
100- foreach ( var item in collection )
101- {
102- sb . Append ( item . Name ) ;
103- sb . Append ( separator ) ;
104- sb . Append ( item . Value ) ;
105-
106- count ++ ;
107- if ( count < total )
108- {
109- sb . Append ( spacer ) ;
110- }
111- }
112-
113- return sb . ToString ( ) ;
114- }
115- }
93+ public static string Concatenate ( this WebParameterCollection collection , string separator , string spacer )
94+ {
95+ var sb = new StringBuilder ( ) ;
96+
97+ var total = collection . Count ;
98+ var count = 0 ;
99+
100+ foreach ( var item in collection )
101+ {
102+ sb . Append ( item . Name ) ;
103+ sb . Append ( separator ) ;
104+ sb . Append ( item . Value ) ;
105+
106+ count ++ ;
107+ if ( count < total )
108+ {
109+ sb . Append ( spacer ) ;
110+ }
111+ }
112+
113+ return sb . ToString ( ) ;
114+ }
115+ }
116116}
0 commit comments