本文整理汇总了C#中System.Collections.Dictionary.Contains方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.Contains方法的具体用法?C# Dictionary.Contains怎么用?C# Dictionary.Contains使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Dictionary
的用法示例。
在下文中一共展示了Dictionary.Contains方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: btnStart_Click
private void btnStart_Click(object sender, RoutedEventArgs e)
{
Task.Run(async () =>
{
bool isOpened = await this.tmp102.OpenAsync();
IDictionary bag = new Dictionary<SensorType, float>();
while (true)
{
float temperature = tmp102.Temperature();
SensorType sensorType = SensorType.Temperature;
if (!bag.Contains(sensorType))
bag.Add(sensorType, temperature);
else
bag[sensorType] = temperature;
if ((this.iotClient != null) && (this.iotClient.IsOpen))
{
this.iotClient.SendAsync(bag);
}
await Task.Delay(5000);
}
});
}
示例2: btnStart_Click
private void btnStart_Click(object sender, RoutedEventArgs e)
{
Task.Run(async () =>
{
IDictionary bag = new Dictionary<SensorType, float>();
var rnd = new Random();
while (true)
{
var temperature = (float)GetRandomNumber(rnd, -10, 50);
SensorType sensorType = SensorType.Temperature;
if (!bag.Contains(sensorType))
bag.Add(sensorType, temperature);
else
bag[sensorType] = temperature;
if ((this.iotClient != null) && (this.iotClient.IsOpen))
{
this.iotClient.SendAsync(bag);
}
await Task.Delay(5000);
}
});
}
示例3: WriterWriteEmptyString
public void WriterWriteEmptyString()
{
string outputPath = Guid.NewGuid().ToString() + ".plist";
IDictionary dict = new Dictionary<string, object>();
dict["Empty"] = string.Empty;
BinaryPlistWriter writer = new BinaryPlistWriter();
writer.WriteObject(outputPath, dict);
BinaryPlistReader reader = new BinaryPlistReader();
dict = reader.ReadObject(outputPath);
Assert.IsTrue(dict.Contains("Empty"));
Assert.AreEqual(string.Empty, dict["Empty"]);
}
示例4: Run
public async void Run(IBackgroundTaskInstance taskInstance)
{
//
// TODO: Insert code to start one or more asynchronous methods
//
BackgroundTaskDeferral deferral = taskInstance.GetDeferral();
this.tmp102 = new TMP102();
// set and open the IoT client
if (this.iotClient == null)
{
//this.iotClient = new IoTClient("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity);
this.iotClient = new IoTClientConnectTheDots("raspberrypi2", Guid.NewGuid().ToString(), this.connectionString, this.eventHubEntity);
}
if (!this.iotClient.IsOpen)
this.iotClient.Open();
bool isOpened = await this.tmp102.OpenAsync();
IDictionary bag = new Dictionary<SensorType, float>();
while (true)
{
float temperature = tmp102.Temperature();
SensorType sensorType = SensorType.Temperature;
if (!bag.Contains(sensorType))
bag.Add(sensorType, temperature);
else
bag[sensorType] = temperature;
if ((this.iotClient != null) && (this.iotClient.IsOpen))
{
this.iotClient.SendAsync(bag);
}
await Task.Delay(5000);
}
deferral.Complete();
}
示例5: IDictionary_Contains
public void IDictionary_Contains ()
{
IDictionary d = new Dictionary<int, int> ();
d.Add (1, 2);
Assert.IsTrue (d.Contains (1));
Assert.IsFalse (d.Contains (2));
Assert.IsFalse (d.Contains ("x"));
}
示例6: Execute
/// <summary>
/// Execute the MSBuild Task.
/// </summary>
/// <returns></returns>
public override bool Execute()
{
_modifiedFiles = new ArrayList();
_deletedFiles = new ArrayList();
// Parse local manifest to retrieve list of files and their hashes.
string[] localManifest = File.ReadAllLines(_localManifestFile);
Dictionary<string, string> localFiles = new Dictionary<string,string>();
for (int i = 0; i < localManifest.Length; i++)
{
if (localManifest[i].StartsWith("Archive-Asset-Name: "))
{
string assetName = localManifest[i].Substring(20);
i++;
if (localManifest[i].StartsWith("Archive-Asset-SHA-512-Digest: "))
{
string assetHash = localManifest[i].Substring(30);
localFiles.Add(assetName, assetHash);
}
else
{
return false;
}
}
}
// Do the same for the target manifest.
string[] targetManifest = File.ReadAllLines(_targetManifestFile);
Dictionary<string, string> targetFiles = new Dictionary<string,string>();
for (int i = 0; i < targetManifest.Length; i++)
{
if (targetManifest[i].StartsWith("Archive-Asset-Name: "))
{
string assetName = targetManifest[i].Substring(20);
i++;
if (targetManifest[i].StartsWith("Archive-Asset-SHA-512-Digest: "))
{
string assetHash = targetManifest[i].Substring(30);
targetFiles.Add(assetName, assetHash);
}
else
{
return false;
}
}
}
// Compare hashes and populate the lists of modified and deleted files.
string[] targetFileMap = File.ReadAllLines(_targetFileMap);
foreach (KeyValuePair<string, string> file in localFiles)
{
// For some reason this MANIFEST.bbr file appears in the manifest, even though
// it doesn't actually exist anywhere...? It doesn't seem to correspond to
// MANIFEST.MF either.
if (file.Key == "META-INF/MANIFEST.bbr")
{
continue;
}
// If the target manifest doesn't contain the same key/value pair,
// that means the local file has been either added or modified.
if (!targetFiles.Contains(file))
{
TaskItem item = new TaskItem(file.Key);
item.SetMetadata("SourcePath", getSourcePath(file.Key, targetFileMap));
_modifiedFiles.Add(item);
}
}
IDictionaryEnumerator targetEnum = targetFiles.GetEnumerator();
while (targetEnum.MoveNext())
{
// If the local manifest doesn't contain the same key,
// that means the target file has been deleted from the project.
if (!localFiles.ContainsKey((string)targetEnum.Key))
{
TaskItem item = new TaskItem((string)targetEnum.Key);
_deletedFiles.Add(item);
}
}
// For some reason the manifest file doesn't show up in the target file map
// or the manifest itself, so we add it manually here and always upload it.
TaskItem manifestItem = new TaskItem("META-INF/MANIFEST.MF");
manifestItem.SetMetadata("SourcePath", "localManifest.mf");
_modifiedFiles.Add(manifestItem);
return true;
}
示例7: Contains_Enumerable_OnDictionary_IsTranslatedAsAContainsResultOperator
public void Contains_Enumerable_OnDictionary_IsTranslatedAsAContainsResultOperator ()
{
var dictionary = new Dictionary<string, int>();
var query = from c in QuerySource
where dictionary.Contains (new KeyValuePair<string, int> (c.Name, c.ID))
select c;
var queryModel = QueryParser.GetParsedQuery (query.Expression);
var whereClause = (WhereClause) queryModel.BodyClauses.Single ();
Assert.That (whereClause.Predicate, Is.InstanceOf<SubQueryExpression> ());
var subQueryModel = ((SubQueryExpression) whereClause.Predicate).QueryModel;
Assert.That (subQueryModel.ResultOperators, Has.Count.EqualTo (1));
Assert.That (subQueryModel.ResultOperators.Single(), Is.TypeOf<ContainsResultOperator>());
}
示例8: Contains_IDictionary_IsNotTranslatedAsAContainsResultOperator
public void Contains_IDictionary_IsNotTranslatedAsAContainsResultOperator ()
{
IDictionary dictionary = new Dictionary<string, int> ();
var query = from c in QuerySource
where dictionary.Contains (c.Name)
select c;
var queryModel = QueryParser.GetParsedQuery (query.Expression);
var whereClause = (WhereClause) queryModel.BodyClauses.Single();
Assert.That (whereClause.Predicate, Is.Not.InstanceOf<SubQueryExpression> ());
Assert.That (whereClause.Predicate, Is.InstanceOf<MethodCallExpression> ());
}
示例9: ReadOnlyDictionary_Unit_GetEnumerator1_Optimal
public void ReadOnlyDictionary_Unit_GetEnumerator1_Optimal()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
ReadOnlyDictionary<String, String> target = new ReadOnlyDictionary<String, String>(dictionary);
using (IEnumerator<KeyValuePair<String, String>> actual = target.GetEnumerator()) {
Int32 count = 0;
while (actual.MoveNext()) {
count++;
Assert.IsTrue(dictionary.Contains(actual.Current));
}
Assert.AreEqual(dictionary.Count, count);
}
}
示例10: SendData
private void SendData()
{
Task.Run(() =>
{
IDictionary bag = new Dictionary<SensorType, float>();
var rate = (float)currentRate;
SensorType sensorType = SensorType.Temperature;
if (!bag.Contains(sensorType))
bag.Add(sensorType, rate);
else
bag[sensorType] = rate;
if ((this.iotClient != null) && (this.iotClient.IsOpen))
{
this.iotClient.SendAsync(bag);
}
});
}
示例11: ShouldLog_IfExceptionDataContainsLoggedByOfIncompatibleType_ReturnsTrue
public void ShouldLog_IfExceptionDataContainsLoggedByOfIncompatibleType_ReturnsTrue()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
IDictionary data = new Dictionary();
data.Add(ExceptionLogger.LoggedByKey, null);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.Equal(true, shouldLog);
Assert.True(data.Contains(ExceptionLogger.LoggedByKey));
object updatedLoggedBy = data[ExceptionLogger.LoggedByKey];
Assert.Null(updatedLoggedBy);
}
示例12: ShouldLog_IfExceptionDataContainsLoggedByLogger_ReturnsFalse
public void ShouldLog_IfExceptionDataContainsLoggedByLogger_ReturnsFalse()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
IDictionary data = new Dictionary();
ICollection<object> loggedBy = new List<object>() { product };
data.Add(ExceptionLogger.LoggedByKey, loggedBy);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.Equal(false, shouldLog);
Assert.True(data.Contains(ExceptionLogger.LoggedByKey));
object updatedLoggedBy = data[ExceptionLogger.LoggedByKey];
Assert.Same(loggedBy, updatedLoggedBy);
Assert.Equal(1, loggedBy.Count);
}
示例13: ShouldLog_IfExceptionDataIsEmpty_AddsLoggedByKeyWithLoggerValueAndReturnsTrue
public void ShouldLog_IfExceptionDataIsEmpty_AddsLoggedByKeyWithLoggerValueAndReturnsTrue()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
IDictionary data = new Dictionary();
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.Equal(true, shouldLog);
Assert.True(data.Contains(ExceptionLogger.LoggedByKey));
object loggedBy = data[ExceptionLogger.LoggedByKey];
Assert.IsAssignableFrom<ICollection<object>>(loggedBy);
Assert.Contains(product, (ICollection<object>)loggedBy);
}
示例14: NonNullDictionary_Unit_GetEnumerator3_Optimal
public void NonNullDictionary_Unit_GetEnumerator3_Optimal()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
IEnumerable target = new NonNullDictionary<String, String>(dictionary);
IEnumerator actual = target.GetEnumerator();
Int32 count = 0;
while (actual.MoveNext()) {
count++;
Assert.IsTrue(dictionary.Contains((KeyValuePair<String, String>)actual.Current));
}
Assert.AreEqual(dictionary.Count, count);
}
示例15: NonNullDictionary_Unit_CopyTo_Optimal
public void NonNullDictionary_Unit_CopyTo_Optimal()
{
IDictionary<String, String> dictionary = new Dictionary<String, String>() {
{ "Key1", "Value1" },
{ "Key2", "Value2" },
{ "Key3", "Value3" }
};
ICollection<KeyValuePair<String, String>> target = new NonNullDictionary<String, String>(dictionary);
KeyValuePair<String, String>[] array = new KeyValuePair<String, String>[dictionary.Count];
Int32 arrayIndex = 0;
target.CopyTo(array, arrayIndex);
Assert.AreEqual(dictionary.Count, array.Length);
foreach (KeyValuePair<String, String> pair in array) {
Assert.IsTrue(dictionary.Contains(pair));
}
}