-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensionMethods.cs
More file actions
44 lines (37 loc) · 1.18 KB
/
Copy pathExtensionMethods.cs
File metadata and controls
44 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SerialPlotAndLog
{
internal class DuplicateKeyException : Exception
{
public object Object { get; protected set; }
public DuplicateKeyException(object duplicate) : this(duplicate, null) { }
public DuplicateKeyException(object duplicate, Exception inner)
: base("Duplicate key encountered.", inner)
{
Object = duplicate;
}
}
static internal class ExtensionMethods
{
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> list)
{
HashSet<T> h = new HashSet<T>();
foreach (T el in list)
if (!h.Add(el))
throw new DuplicateKeyException(el);
return h;
}
public static string Nullify(this string s)
{
return string.IsNullOrEmpty(s) ? null : s;
}
public static string Join(this IEnumerable<string> strings, string delimiter)
{
return string.Join(delimiter, strings as string[] ?? strings.ToArray()).Nullify();
}
}
}