Вопрос

I have a Dictionary property

 public Dictionary<string, bool> SearchType { get; set; }

this Dictionary has 4 keys and 4 values for that keys. Now I take them to a variable from SearchType, if the values are true

var searchTypes = searchDetail.SearchType.Where(x => x.Value == true).ToList();

Here I checked the key is CKBinstituteType or CKBstate and etc, from the below code

foreach (var searchType in searchTypes)
{
    if (searchType.Key.Contains("CKBinstituteType"))
    {

    }
    if (searchType.Key.Contains("CKBstate"))
    {

    }
    if (searchType.Key.Contains("CKBlocation"))
    {

    }
    if (searchType.Key.Contains("CKBdistance"))
    {

    }
 }

or tried with this way (used equal operation instead of contains)

foreach (var searchType in searchTypes)
{
    if (searchType.Key == "CKBinstituteType")
    {

    }
    if (searchType.Key == "CKBstate")
    {

    }
    if (searchType.Key == "CKBlocation")
    {


    }
    if (searchType.Key == "CKBdistance")
    {

    }                   
}

What is the difference between of them? Which one is good for my situation? (like performance, code standard,etc)

Это было полезно?

Решение

What is the different between of them?

Both, Contains and Equals are using string comparison. Since your Key is of type string, Contains will check if the passed parameter is part of the key, whereas Equals compares the complete string for equality.

Which one is good for my situation? (like performance, code standard,etc)

Use ContainsKey method , instead of string equals or contains. Contains and Equals are used inside the foreach loop, where you are comparing the Key which is a string with Contains and Equals. You don't need to iterate each item in the dictionary. If you are trying to access it through Key, It is about doing a Linear search with complexity of O(n) vs doing dictionary lookup with complexity O(1)

You can use ContainsKey Like

if (SearchType.ContainsKey("CKBinstituteType"))
{
}

Currently you are converting your Dictionary to List, I am not sure if there is really a need your dictionary to a List and then do a linear search. If you really have to filter out the Dictionary based on true values then project the result set into a dictionary and then use ContainsKey like:

var searchTypes = searchDetail.SearchType.Where(r => r.Value == true)
                            .ToDictionary(r => r.Key, r => r.Value);
if (searchTypes.ContainsKey("CKBinstituteType"))
{

}
if (searchTypes.ContainsKey("CKBstate"))
{

}
if (searchTypes.ContainsKey("CKBlocation"))
{


}
if (searchTypes.ContainsKey("CKBdistance"))
{

}

Другие советы

The difference is that

stringExpr.Contains("CKBinstituteType")

checks if there is some substring of stringExpr which is equal to "CKBinstituteType", while

stringExpr == "CKBinstituteType"

checks if stringExpritself is equal to "CKBinstituteType".

In both cases an ordinal comparison (which is culture invariant) is used. As an explicit example

"xyzxyzCKBinstituteTypexyzxyzxyz".Contains("CKBinstituteType")

is true while

"xyzxyzCKBinstituteTypexyzxyzxyz" == "CKBinstituteType"

is false.


Performance: stringExpr.Contains("CKBinstituteType") will be slower than stringExpr == "CKBinstituteType". Note that I am not addressing the fact that these strings are keys in a Dictionary<string, Something>. See Habib's answer. A Dictionary<,> offers fast (O(1)) lookup on key. You don't utilize that when you do dict.Where(x => criterion).ToList().

contains() checks for a sub string , where as equals() checks for whole string , and other difference is , contains() takes object of CharSequence class where as equals() takes Object as its parameter

Regarding simply whether using Compare or Equals on equivalent strings makes any difference:

Using ReSharper to see the source code for Compare, the first thing it does is

if ((Object)strA == (Object)strB) 
{ 
    return 0;
} 

So it looks like they would essentially be the same for equivalent strings.

As far as performance, this blog discusses the difference, although the comments include the most useful comparisons. Most commentators seemed to find negligible difference even after running them in very large loops. The last comment though shows a large difference between Equals and Contains and makes me wonder why the results didn't more closely mirror the others.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top