Photo Gallery

Wednesday, June 28, 2017

How to check if string value exists in a list using C#

In this post, I will explain with an example, how to check string value exist in a list using C# and it will be quicker than LINQ.


here, string comparison specifies the culture, case and sort rules to be used by string methods.

/// <summary>
/// Method to check if a string value exists in a list. Will be quicker than Linq.
/// </summary>
/// <param name="list">The list of strings.</param>
/// <param name="match">The item to match.</param>
/// <param name="comparisonType">The type of comparison to perform.</param>
/// <returns>True/false indication of whether the item exists in the list.</returns>
private bool ListMatch(IList<string> list, string match, StringComparison comparisonType)
{
bool result = false;
if ((list != null) && (list.Count > 0))
{
foreach (string item in list)
{
int compare = string.Compare(item, match, comparisonType);
if (compare == 0)
{
result = true;
}
}
}
return result;
}
view raw Utilities.cs hosted with ❤ by GitHub



If you have any questions, post here. Also, provide your valuable suggestions and thoughts in form of comments in the section below.

Thank you !!!

No comments:

Post a Comment