My favorites
▼
|
Sign in
dotnetsetdefault
C# SetDefault / .Net
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
SetDefaultExtension.cs
r2
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SandPit.Shaun.Algorithms.Collections
{
/// <summary>
/// These extension methods attempt to emulate the SetDefault method available to
/// python dictionaries. While they emulate them in spirit, they are probably
/// not as perfomant
///
/// I decided to use the TryGetValue approach - though
/// try/catch/KeyNotFound or Contains/[] approach might be best depending on specific
/// requirements (how often you expect to find the key already there etc)
///
/// Author: Shaun McCarthy - http://www.shaunmccarthy.com (me@shaunmccarthy.com)
/// </summary>
public static class SetDefaultExtension
{
/// <summary>
/// This method checks to see if a key exists in the dictionary. If so, it returns
/// that key, otherwise it returns a default value (and sets that default value to
/// be the value in the dictionary)
/// </summary>
/// <typeparam name="TKey">The key type of the generic dictionary</typeparam>
/// <typeparam name="TValue">The value type of the generic dictionary</typeparam>
/// <param name="dictionary">Refernce to the dictionary you are using</param>
/// <param name="key">The key to look for</param>
/// <param name="defaultValue">The value to insert and use if it's not there</param>
/// <returns>The value associated with the key</returns>
public static TValue SetDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue)
{
TValue result;
// If we can't find the value, return the default
if (!dictionary.TryGetValue(key, out result))
return dictionary[key] = defaultValue;
return result;
}
/// <summary>
/// This method checks to see if a key exists in the dictionary. If so, it returns
/// that key, otherwise it creates a default value using the function passed in
/// (and sets that default value to be the value in the dictionary)
/// </summary>
/// <typeparam name="TKey">The key type of the generic dictionary</typeparam>
/// <typeparam name="TValue">The value type of the generic dictionary</typeparam>
/// <param name="dictionary">Refernce to the dictionary you are using</param>
/// <param name="key">The key to look for</param>
/// <param name="initFunction">The method to use to create the value to insert and use if it's not there</param>
/// <returns>The value associated with the key</returns>
public static TValue SetDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> initFunction)
{
TValue result;
// If we can't find the value, run the default generation method
if (!dictionary.TryGetValue(key, out result))
{
if (initFunction == null)
{
throw new Exception("You haven't defined an initialization function with SetDefaultFunc");
}
return dictionary[key] = initFunction();
}
return result;
}
/// <summary>
/// This method checks to see if a key exists in the dictionary. If so, it returns
/// that key, otherwise it creates a default value using Activator.CreateInstance
///
/// NB: This calls the default constructor on the object - if it doesn't exist,
/// this will throw a MissingMethodException
/// </summary>
/// <typeparam name="TKey">The key type of the generic dictionary</typeparam>
/// <typeparam name="TValue">The value type of the generic dictionary</typeparam>
/// <param name="dictionary">Refernce to the dictionary you are using</param>
/// <param name="key">The key to look for</param>
/// <param name="initFunction">The method to use to create the value to insert and use if it's not there</param>
/// <returns>The value associated with the key</returns>
public static TValue SetDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
{
TValue result;
if (!dictionary.TryGetValue(key, out result))
{
return dictionary[key] = (TValue)Activator.CreateInstance(typeof(TValue));
}
return result;
}
}
}
Show details
Hide details
Change log
r2
by shaunmccarthy on Aug 6, 2010
Diff
[No log message]
Go to:
/trunk/SetDefaultExtension.cs
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 4645 bytes, 90 lines
View raw file
Powered by
Google Project Hosting