2017-07-18 38 views
1

使用C#我如何確保下面代碼中的'val'對象是小寫?將對象鍵轉換爲小寫

case MultiValueUpdateMode.AddIfNotExist: 
if (value != null) 
{ 
    // Add the values only if they do not exist 
    foreach (var val in newValues) 
    { 
     if (!userDe.Properties["proxyAddresses"].Contains(val)) 
     userDe.Properties["proxyAddresses"].Add(val); 
    } 
} 
break; 
+0

你沒有給我們足夠的信息,但是你可以使用ToLower將()擴展方法的字符串。 –

回答

1

您可以通過使用ToString()轉換任何對象的字符串,然後使用ToLower()讓它是小寫字母串:

string lowerCasedString = val?.ToString()?.ToLower(); //put null coaelescing just in case it is null 

然後使用它是這樣的:

if (!userDe.Properties["proxyAddresses"].Contains(lowerCasedString)) 

附加說明:我通常也會刪除前導和後續whi TE-空間利用Trim()

string cleanLowerCasedString = val?.ToString()?.ToLower()?.Trim(); //put null coaelescing just in case it is null 
+0

謝謝你,工作。 – user1226884

0
case MultiValueUpdateMode.AddIfNotExist: 
if (value != null) 
    { 
    // Add the values only if they do not exist 
    foreach (var val in newValues) 
    { 
     var lowerCaseVal = val.ToLower(); 
     if (!userDe.Properties["proxyAddresses"].Contains(lowerCaseVal)) 
     userDe.Properties["proxyAddresses"].Add(lowerCaseVal); 
    } 
    } 
break; 
+0

當我添加val = val.ToLower();你在哪裏建議我得到intellisense錯誤'不能分配給'val',因爲它是'foreach迭代變量'。我錯過了別的嗎? – user1226884

+0

@ user1226884更新。謝謝 – Ramankingdom