本文整理汇总了C#中Dictionary.AddOrUpdate方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.AddOrUpdate方法的具体用法?C# Dictionary.AddOrUpdate怎么用?C# Dictionary.AddOrUpdate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dictionary
的用法示例。
在下文中一共展示了Dictionary.AddOrUpdate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ParseCommandLine
public static Dictionary<string, string> ParseCommandLine(string[] args)
{
var arguments = new Dictionary<string, string>();
// App.Config Settings
var appSettingKeys = ConfigurationManager.AppSettings.Keys;
for (var i = 0; i < appSettingKeys.Count; i++)
{
var key = appSettingKeys[i];
arguments.AddOrUpdate(key, ConfigurationManager.AppSettings[key]);
}
// Manual override through CLI.
var p = new OptionSet()
{
{
"<>", v =>
{
if (!v.StartsWith("--"))
return;
var split = v.Split(new[] { '=' }, 2);
if (split.Length != 2)
return;
arguments.AddOrUpdate(split[0].TrimStart('-'), split[1]);
}
}
};
p.Parse(args);
return arguments;
}
示例2: AddOrUpdateOverloadOne
public void AddOrUpdateOverloadOne()
{
var dictionary = new Dictionary<string, string>();
Func<string, string, string> updater = (key, oldValue) =>
{
key.Should().Be("fookey");
oldValue.Should().Be("foo");
return "bar";
};
Func<string, string, string> nullUpdater = null;
dictionary.AddOrUpdate(key: "fookey", addValue: "foo", updateValueFactory: updater).Should().Be("foo");
dictionary["fookey"].Should().Be("foo");
dictionary.AddOrUpdate(key: "fookey", addValue: "foo", updateValueFactory: updater).Should().Be("bar");
dictionary["fookey"].Should().Be("bar");
// Null argument checks
Action action = action = () => dictionary.AddOrUpdate(key: null, addValue: null, updateValueFactory: nullUpdater);
action.ShouldThrow<ArgumentNullException>()
.And.ParamName.Should().Be("key");
action = () => dictionary.AddOrUpdate(key: "foo", addValue: null, updateValueFactory: nullUpdater);
action.ShouldThrow<ArgumentNullException>()
.And.ParamName.Should().Be("updateValueFactory");
dictionary = null;
action.ShouldThrow<ArgumentNullException>()
.And.ParamName.Should().Be("source");
}
示例3: Main
static void Main(string[] args)
{
var arguments = new Dictionary<string, string>();
// App.Config Settings
var appSettingKeys = ConfigurationManager.AppSettings.Keys;
for (var i = 0; i < appSettingKeys.Count; i++)
{
var key = appSettingKeys[i];
arguments.AddOrUpdate(key, ConfigurationManager.AppSettings[key]);
}
// Manual override through CLI.
var p = new OptionSet()
{
{
"<>", v =>
{
if (!v.StartsWith("--"))
return;
var split = v.Split(new[] { '=' }, 2);
if (split.Length != 2)
return;
arguments.AddOrUpdate(split[0].TrimStart('-'), split[1]);
}
}
};
p.Parse(args);
var loader = new AssemblyLoader();
var dbProvider = loader.CreateTypeFromAssembly<DbProvider>(arguments["dbp.provider"], arguments);
var dbCodeFormatter = loader.CreateTypeFromAssembly<DbTraceCodeFormatter>(arguments["tcf.provider"], arguments);
var codeHighlighter = loader.CreateTypeFromAssembly<HighlightCodeProvider>(arguments["hcp.provider"], arguments);
var outputProvider = loader.CreateTypeFromAssembly<OutputProvider>(arguments["out.provider"], arguments);
switch (arguments["app.mode"].ToLower().Trim())
{
case "generate":
var generateCommand = new GenerateOutputCommand(dbProvider, dbCodeFormatter, codeHighlighter, outputProvider, arguments["app.traceName"]);
generateCommand.Execute();
break;
case "start":
var startCommand = new StartCommand(outputProvider, dbProvider, arguments["app.traceName"]);
startCommand.Execute();
break;
case "stop":
{
var stopCommand = new StopCommand(dbProvider, outputProvider, arguments["app.traceName"]);
stopCommand.Execute();
}
break;
}
}
示例4: AddOrUpdateTest_CommonValue_Exist
public void AddOrUpdateTest_CommonValue_Exist()
{
IDictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.AddOrUpdate("key", "value");
Assert.AreEqual(1, dictionary.Count);
dictionary.AddOrUpdate("key", "newValue");
Assert.AreEqual(1, dictionary.Count);
Assert.IsTrue(dictionary.ContainsKey("key"));
Assert.AreEqual("newValue", dictionary["key"]);
}
示例5: AddOrUpdate
public void AddOrUpdate()
{
// Type
var @this = new Dictionary<string, string>();
// Examples
string value1 = @this.AddOrUpdate("Fizz", "Buzz"); // return "Buzz";
string value2 = @this.AddOrUpdate("Fizz", "Buzz2"); // return "Buzz2";
// Unit Test
Assert.AreEqual("Buzz", value1);
Assert.AreEqual("Buzz2", value2);
}
示例6: AddOrUpdateTest_ListValue_Exist
public void AddOrUpdateTest_ListValue_Exist()
{
IDictionary<string, IList<string>> dictionary = new Dictionary<string, IList<string>>();
dictionary.AddOrUpdate("key", "value");
Assert.AreEqual(1, dictionary.Count);
dictionary.AddOrUpdate("key", "newValue");
Assert.AreEqual(1, dictionary.Count);
Assert.IsTrue(dictionary.ContainsKey("key"));
Assert.IsNotNull(dictionary["key"]);
Assert.AreEqual(2, dictionary["key"].Count);
Assert.AreEqual("value", dictionary["key"].First());
Assert.AreEqual("newValue", dictionary["key"].Last());
}
示例7: AddOrUpdateTestCase1
public void AddOrUpdateTestCase1()
{
var key = RandomValueEx.GetRandomString();
var pair = new KeyValuePair<String, String>( key, RandomValueEx.GetRandomString() );
var dic = new Dictionary<String, String>();
var result = dic.AddOrUpdate( pair );
Assert.AreEqual( 1, dic.Count );
Assert.AreEqual( pair.Value, result );
pair = new KeyValuePair<String, String>( key, RandomValueEx.GetRandomString() );
result = dic.AddOrUpdate( pair );
Assert.AreEqual( 1, dic.Count );
Assert.AreEqual( pair.Value, result );
}
示例8: AddOrUpdateTestCase2
public void AddOrUpdateTestCase2()
{
var key = RandomValueEx.GetRandomString();
var dic = new Dictionary<String, String>();
var valueToAdd = RandomValueEx.GetRandomString();
var result = dic.AddOrUpdate( key, () => valueToAdd );
Assert.AreEqual( 1, dic.Count );
Assert.AreEqual( valueToAdd, result );
valueToAdd = RandomValueEx.GetRandomString();
result = dic.AddOrUpdate( key, () => valueToAdd );
Assert.AreEqual( 1, dic.Count );
Assert.AreEqual( valueToAdd, result );
}
示例9: DoTryGetSourceControlInfo
protected override bool DoTryGetSourceControlInfo(string solutionFullPath, out IDictionary<string, object> properties)
{
properties = new Dictionary<string, object>();
if (!EnsureInternalSetup(solutionFullPath, ref properties))
return false;
var head_path = (string)properties.GetValueOrDefault("git:headPath", () => "");
if (!File.Exists(head_path))
return false;
var head_content = File.ReadAllText(head_path);
var branch_match = GitBranchRegex.Match(head_content);
if (!branch_match.Success)
return false;
var branch_group = branch_match.Groups["branchName"];
if (!branch_group.Success)
return false;
var branch_name = branch_group.Value;
properties.Add("git:head", branch_name);
properties.AddOrUpdate(KnownProperties.BranchName, branch_name);
return true;
}
示例10: Update
public void Update()
{
var keyedValues = new Dictionary<string, Value> { { "a", new Value { Data = "b" } } };
keyedValues.AddOrUpdate("a", null, _ => _.Data = "c");
Assert.AreEqual("c", keyedValues["a"].Data);
}
示例11: Add
public void Add()
{
var keyedValues = new Dictionary<string, Value>();
keyedValues.AddOrUpdate("a", () => new Value { Data = "b" }, null);
Assert.AreEqual("b", keyedValues["a"].Data);
}
示例12: TestRandomness
public void TestRandomness(Func<List<int>> getRandomList, int rangeLength, int count)
{
var combinations = rangeLength.Factorial(rangeLength - count + 1);
var iterations = combinations * 100;
var ocurrences = new ConcurrentDictionary<long, int>(Environment.ProcessorCount, (int)combinations);
var partitioner = Partitioner.Create(0, (long)iterations);
Parallel.ForEach(partitioner, new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount},
range =>
{
//hopefully having a private dictionary will help concurrency
var privateOcurrences = new Dictionary<long, int>();
for (long i = range.Item1; i < range.Item2; ++i)
{
var list = getRandomList();
//this will only work when numbers are between 0 and 88
long acc = list.Aggregate<int, long>(0, (current, value) => (value + 11) + current*100);
privateOcurrences.AddOrUpdate(acc, 1, v => v + 1);
}
foreach (var privateOcurrence in privateOcurrences)
{
ocurrences.AddOrUpdate(privateOcurrence.Key,
privateOcurrence.Value,
(k, v) => v + privateOcurrence.Value);
}
});
var averageOcurrences = iterations / (combinations * 1.0m);
var currentAverage = ocurrences.Values.Average();
Debug.WriteLine("The average ocurrences of this implementation is {0} comparing to {1} in the 'perfect' scenario", currentAverage, averageOcurrences);
Assert.Less(currentAverage, averageOcurrences * 1.05m);
Assert.Greater(currentAverage, averageOcurrences * 0.95m);
}
示例13: AddOrUpdate
public void AddOrUpdate()
{
var dictionary = new Dictionary<string, string>();
Action action = () => dictionary.AddOrUpdate(null, "");
action.ShouldThrow<ArgumentNullException>()
.And.ParamName.Should().Be("key");
dictionary = null;
action.ShouldThrow<ArgumentNullException>()
.And.ParamName.Should().Be("source");
var dictionaryTwo = new Dictionary<int, int>();
dictionaryTwo.Add(1, 1);
dictionaryTwo.AddOrUpdate(1, 2);
dictionaryTwo[1].Should().Be(2);
dictionaryTwo.AddOrUpdate(2, 3);
dictionaryTwo[2].Should().Be(3);
}
示例14: FindAllListingsForProduct
private static void FindAllListingsForProduct(Product product, List<Listing> listings, Dictionary<string, List<Listing>> allListingsOfProduct)
{
foreach (var listing in listings)
{
if (listing == null || listing.Picked || !ListingMatchesProduct(product.Tokens, listing.Tokens))
continue;
listing.Picked = true;
allListingsOfProduct.AddOrUpdate(product.ProductName, listing);
}
}
示例15: DoTryGetSourceControlInfo
protected override bool DoTryGetSourceControlInfo(string solutionFullPath, out IDictionary<string, object> properties)
{
properties = new Dictionary<string, object>();
if (!TeamFoundationHelper.IsConnected())
return false;
var workspace = (Workspace) null;
var branchObject = (BranchObject)null;
var branchName = TeamFoundationHelper.GetBranchNameForItem(solutionFullPath, out workspace, out branchObject);
if (branchName.IsNullOrEmpty())
return false;
properties.AddOrUpdate("tfs:workspace", workspace.Name);
properties.AddOrUpdate(KnownProperties.BranchName, branchName);
return true;
}