本文整理汇总了C#中Dictionary.ContainsValue方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.ContainsValue方法的具体用法?C# Dictionary.ContainsValue怎么用?C# Dictionary.ContainsValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Dictionary
的用法示例。
在下文中一共展示了Dictionary.ContainsValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: collections_example
public collections_example()
{
//rule of thumb: generics exist and are a wonderful thing.
//Lists
List<string> sList = new List<string>();
var namesList = new List<string>();//this is implying that var is a List<string>. It shortens variable declarations. This cannot be used as a return type. Variables only.
sList.Add("James");//this is rather time consuming and not helpful. You are usually adding this from a db or data file.
//you can add arrays to collections.
string[] names = new string[]{
"James",
"John",
"Jennifer",
"Jackie",
"Jake",
"Jeremy"
};
namesList.AddRange(names);//this adds all the names in an array
Console.WriteLine("The number of items in namesList is "+namesList.Count); //checks how many values are in the collection.
bool james = namesList.Contains("James");//would return true.
bool katie = namesList.Contains("Katie");// returns false
Console.WriteLine("Does it contain James? {0}\nDoes it contain Katie? {1}\n", james, katie);
var newList = namesList.FindAll((s) => s[1] == 'a');//lamba expressions, quickest way to write a predicate. More on this in future lessons.
//copies the name whose second letter contains an a
Console.WriteLine(newList.ToArray());//prints the name of the array, not the names IN the array.
foreach (var name in newList)
{
Console.WriteLine(name);
}
//there's all sorts of stuff you can do to these. Remove, add, skip, sort, foreach, and far more. Use them on an as-needed basis.
namesList.Clear();//clears everything so it is back at 0. Good for "cleaning the slate". YOU CANNOT GET THEM BACK!
//Dictionaries
Dictionary<int, string> dict = new Dictionary<int, string>();//could use var as well.
dict.Add(0, "Jeremy");//keys must be unique. Int is the key.
dict.Add(1, "Jake");// this changes the key value.
dict.Add(2, "Hanna");
string value = dict[0];//Jeremy
Console.WriteLine("The value of value is: " + value);
dict.ContainsValue("0");//returns true
dict.ContainsValue("9");//returns false
dict.Remove(1);//usually use a foreach loop to remove all values.
foreach (var key in dict)
{
Console.WriteLine(key.Key + " = " + key.Value);
}
}
示例2: Catagorize
public override HandRanking Catagorize(Hand hand)
{
Dictionary<Value, int> seen = new Dictionary<Value, int>();
foreach (Card c in hand.Cards)
{
if (seen.ContainsKey(c.Value))
{
seen[c.Value]++;
}
else
{
seen[c.Value] = 1;
}
}
if (seen.Count == 2)
{
if(seen.ContainsValue(3) && seen.ContainsValue(2))
{
return HandRanking.FullHouse;
}
}
return Next.Catagorize(hand);
}
示例3: IsFourOfAKind
public bool IsFourOfAKind(IHand hand)
{
if (!this.IsValidHand(hand))
{
return false;
}
Dictionary<CardFace, int> faces = new Dictionary<CardFace, int>();
for (var i = 0; i < NumberOfCardsInHand; i++)
{
if (!faces.ContainsKey(hand.Cards[i].Face))
{
faces.Add(hand.Cards[i].Face, 1);
}
else
{
faces[hand.Cards[i].Face] = faces[hand.Cards[i].Face] + 1;
}
}
if (faces.Count == 2 && faces.ContainsValue(4) && faces.ContainsValue(1) && faces.Count == 2)
{
return true;
}
else
{
return false;
}
}
示例4: TheIdMapWillContainAMapOfAllChildren
public void TheIdMapWillContainAMapOfAllChildren()
{
TestHierarchy h = new TestHierarchy();
Dictionary<int, UnityObject> ids = new Dictionary<int, UnityObject>();
h.root.SetNewId(ids);
Assert.That(ids.ContainsValue(h.root), Is.True);
Assert.That(ids.ContainsValue(h.child), Is.True);
Assert.That(ids.ContainsValue(h.childOfChild), Is.True);
}
示例5: TheIdMapWillContainTheObjectAndItsOriginalId
public void TheIdMapWillContainTheObjectAndItsOriginalId()
{
Dictionary<int, UnityObject> ids = new Dictionary<int, UnityObject>();
GameObject go = new GameObject();
go.SetNewId(ids);
Assert.That(ids.Count, Is.EqualTo(2));
Assert.That(ids.ContainsValue(go), Is.True);
Assert.That(ids.ContainsValue(go.transform), Is.True);
}
示例6: Main
static void Main(string[] args)
{
//Get the string from the application config
var initialWord = ConfigurationManager.AppSettings["initialString"];
initialWord = initialWord.ToLower();
var initialWordFrequence=Class1.getFrequenceVector(initialWord);
//The words from the file are kept in a dictionary, together with their frequence vector
var wordsFromFileDict = new Dictionary<string, Dictionary<char, int>>();
//The result strings are kept in a dictionary
var resultDict = new Dictionary<string, string>();
var reader = new StreamReader("wordList.txt");
var line = "";
while (line != null)
{
line = reader.ReadLine();
if (line == null)
break;
line = line.ToLower();
if(!wordsFromFileDict.ContainsKey(line))
wordsFromFileDict.Add(line, Class1.getFrequenceVector(line));
}
foreach (var x in wordsFromFileDict)
{
//Console.WriteLine(x.ToLower());
foreach (var y in wordsFromFileDict)
{
initialWordFrequence = Class1.getFrequenceVector(initialWord);
if (Class1.canBeAnagrams(x.Value, y.Value, initialWordFrequence))
if (Class1.areAnagrams(x.Value, y.Value, initialWordFrequence))
//Doesn't add duplicates in the result
if(!(resultDict.ContainsKey(x.Key) && resultDict.ContainsValue(y.Key))
&& !(resultDict.ContainsKey(y.Key) && resultDict.ContainsValue(x.Key) ))
resultDict.Add(x.Key, y.Key);
}
}
//Print the results
foreach (var item in resultDict)
{
Console.WriteLine(String.Format("Anagrams of {0} : {1} - {2}", initialWord, item.Key, item.Value));
}
Console.ReadLine();
}
示例7: agregarUsuariosMismoNombreDiccionario
public void agregarUsuariosMismoNombreDiccionario()
{
Dictionary<int,UsuarioView> usuarios = new Dictionary<int, UsuarioView>();
UsuarioView a = new UsuarioView(1, "a", "b", "c");
UsuarioView c = new UsuarioView(2, "a", "b", "c");
usuarios.Add(int.Parse(a.id), a);
usuarios.Add(int.Parse(c.id), c);
bool expected = true;
bool expected1 = usuarios.ContainsValue(a);
bool expected2 = usuarios.ContainsValue(c);
Assert.AreEqual(expected1,expected);
Assert.AreEqual(expected2, expected);
}
示例8: GetDataV2
public static async Task<string> GetDataV2(RequestMethod method, string command, Dictionary<string, string> reqParams)
{
string json;
if (reqParams != null && (reqParams.ContainsValue("True") || reqParams.ContainsValue("False")))
{
json = JsonConvert.SerializeObject(BoolDictionary(reqParams), Formatting.None);
}
else
{
json = JsonConvert.SerializeObject(reqParams, Formatting.None);
}
return await GetDataV2WithJson(method, command, json);
}
示例9: Main
public static void Main(string[] args)
{
/* List Operations */
List<int> list = new List<int>();
list.Add (10);
list.AddRange (new List<int> () { 20, 30, 40 });
list.Remove (10);
list.Insert (0, 10);
list.Clear ();
/* Dictionary Operations */
Dictionary<int, string> dictionary = new Dictionary<int, string> ();
dictionary.Add (1, "Sanjana");
dictionary.Add (2, "Sumana");
dictionary.ContainsValue ("Sanjana");
dictionary.Remove (1);
dictionary.Add (3, "Srinivas");
dictionary.Clear ();
}
示例10: Main
static void Main(string[] args)
{
// Task: to convert the Dictionary collection "elements"
//to List with keeping hierarchy of elements
Dictionary<string, string> elements = new Dictionary<string, string>()
{
{"Tissue", "Organ"},
{"Cell", "Cells"},
{"System", "Body"},
{"Cells", "Tissue"},
{"Organ", "System"},
};
List<string> hierarchy = new List<string>();
string first = elements.Keys.First(el => !elements.ContainsValue(el));
hierarchy.Add(first);
while (elements.ContainsKey(hierarchy.Last()))
hierarchy.Add(elements[hierarchy.Last()]);
foreach (var item in hierarchy)
Console.WriteLine(item);
Console.ReadKey();
}
示例11: IsIsomorphic
private bool IsIsomorphic(string s, string t)
{
if (string.IsNullOrEmpty(s) && string.IsNullOrEmpty(t)) return true;
int slength = s.Length;
int tlength = t.Length;
if (string.IsNullOrEmpty(s) || string.IsNullOrEmpty(t) || slength != tlength) return false;
Dictionary<char, char> dic = new Dictionary<char, char>();
for (int index = 0; index < slength; index++)
{
if (dic.ContainsKey(s[index]))
{
if (dic[s[index]] != t[index])
{
return false;
}
}
else
{
if (dic.ContainsValue(t[index]))
{
return false;
}
else
{
dic.Add(s[index], t[index]);
}
}
}
return true;
}
示例12: PosTest2
public bool PosTest2()
{
bool retVal = true;
// Add your scenario description here
TestLibrary.TestFramework.BeginScenario("PosTest2: Verify method ContainsValue(TValue) when the dictionary contains specified value .");
try
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add("txt", "notepad.exe");
bool actual = dictionary.ContainsValue("notepad.exe");
bool expected = true;
if (actual != expected)
{
TestLibrary.TestFramework.LogError("001.1", "Method ContainsValue(TValue) Err.");
TestLibrary.TestFramework.LogInformation("WARNING [LOCAL VARIABLE] actual = " + actual + ", expected = " + expected);
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("001.2", "Unexpected exception: " + e);
TestLibrary.TestFramework.LogInformation(e.StackTrace);
retVal = false;
}
return retVal;
}
示例13: UniqueNumber
private static string UniqueNumber(int[] arr)
{
string result = string.Empty;
if (arr != null && arr.Length > 0 && arr.Length % 2 != 0)
{
Dictionary<int, int> count = new Dictionary<int, int>();
for (int i = 0; i < arr.Length; i++)
{
if (count.ContainsKey(arr[i]))
{
count[arr[i]]++;
}
else
{
count.Add(arr[i], 1);
}
}
if (count.ContainsValue(1))
{
foreach (var item in count)
{
if (item.Value == 1)
{
result = item.Key.ToString();
break;
}
}
}
}
return result;
}
示例14: Main
public static void Main(string[] args)
{
/* List Operations */
List<int> list = new List<int>();
list.Add (1);
list.Add (2);
list.Add (3);
list.Remove (1);
list.Insert (0, 20);
Console.WriteLine ("before clearing the list {0}",list.Count);
list.Clear ();
Console.WriteLine ("afetr clearing the list {0}",list.Count);
/* Dictionary Operations */
Dictionary<int, string> dictionary = new Dictionary<int, string> ();
dictionary.Add (1, "xxx");
dictionary.Add (2, "yyy");
dictionary.ContainsValue ("yyy");
dictionary.Remove (2);
dictionary.Add (3, "zzz");
Console.WriteLine ("before clearing the dictionary {0}",dictionary.Count);
dictionary.Clear ();
Console.WriteLine ("after clearing the dictionary {0}",dictionary.Count);
}
示例15: PublishReusableWorkflow
/// <summary>
/// Publish Reusable Workflow
/// </summary>
/// <param name="mapping"></param>
public void PublishReusableWorkflow(NWFMappingEntry mapping)
{
SPContentType ct = web.ContentTypes[mapping.BindingName];
string workflowName = mapping.WorkflowName;
string pathToNWF = Path.Combine(properties.Definition.RootDirectory, mapping.WorkflowFileName);
byte[] workflowData = File.ReadAllBytes(pathToNWF);
string workflowFile = Utility.ConvertByteArrayToString(workflowData);
while ((int)workflowFile[0] != (int)char.ConvertFromUtf32(60)[0])
workflowFile = workflowFile.Remove(0, 1);
ExportedWorkflowWithListMetdata workflowWithListMetdata = ExportedWorkflowWithListMetdata.Deserialize(workflowFile);
string xmlMessage = workflowWithListMetdata.ExportedWorkflowSeralized;
SPListCollection lists = web.Lists;
Dictionary<string, Guid> dictionary = new Dictionary<string, Guid>(lists.Count);
foreach (SPList spList in lists)
{
if (!dictionary.ContainsKey(spList.Title.ToUpper()))
dictionary.Add(spList.Title.ToUpper(), spList.ID);
}
foreach (var listReference in workflowWithListMetdata.ListReferences)
{
string key = listReference.ListName.ToUpper();
if (dictionary.ContainsKey(key) && !dictionary.ContainsValue(listReference.ListId))
xmlMessage = xmlMessage.Replace(Utility.FormatGuid(listReference.ListId), Utility.FormatGuid(dictionary[key]));
}
var exportedWorkflow = WorkflowPart.Deserialize<ExportedWorkflow>(xmlMessage);
foreach (var config in exportedWorkflow.Configurations.ActionConfigs)
WorkflowRenderer.ProcessActionConfig(config);
Guid listId = Guid.Empty;
bool validateWorkflow = true;
Publish publish = new Publish(web);
publish.PublishAWorkflow(workflowName, exportedWorkflow.Configurations, listId, web, (ImportContext)null, validateWorkflow, ct.Id, string.Empty);
}