DotNETWeekly-io / DotNetWeekly

DotNet weekly newsletter
MIT License
198 stars 3 forks source link

【文章推荐】𝐂#/.𝐍𝐄𝐓 𝐏𝐞𝐫𝐟𝐨𝐫𝐦𝐚𝐧𝐜𝐞 𝐓𝐢𝐩 - 𝐓𝐨𝐔𝐩𝐩𝐞𝐫()/𝐓𝐨𝐋𝐨𝐰𝐞𝐫() 𝐯𝐬 𝐬𝐭𝐫𝐢𝐧𝐠.𝐄𝐪𝐮𝐚𝐥𝐬() #687

Closed tonyqus closed 1 day ago

tonyqus commented 2 weeks ago

image

💡 You should prefer 𝐬𝐭𝐫𝐢𝐧𝐠.𝐄𝐪𝐮𝐚𝐥𝐬() instead of 𝐓𝐨𝐔𝐩𝐩𝐞𝐫()/𝐓𝐨𝐋𝐨𝐰𝐞𝐫() methods when comparing strings.

⚡ The 𝗧𝗼𝗨𝗽𝗽𝗲𝗿()/𝗧𝗼𝗟𝗼𝘄𝗲𝗿() methods using for case conversion in .NET can impact performance due to memory allocation, string copying, and potential garbage collection, especially in situations involving large strings or frequent conversions, as you can see in example.

✅ Performance-wise, 𝐬𝘁𝗿𝗶𝗻𝗴.𝗘𝗾𝘂𝗮𝗹𝘀() is faster than 𝐓𝐨𝐔𝐩𝐩𝐞𝐫() or 𝐓𝐨𝐋𝐨𝐰𝐞𝐫() due to direct character comparison, avoiding memory allocation, and reducing overhead for case-insensitive string comparison.

🚀 𝐏.𝐬: 𝐒𝐭𝐫𝐢𝐧𝐠𝐂𝐨𝐦𝐩𝐚𝐫𝐢𝐬𝐨𝐧.𝐎𝐫𝐝𝐢𝐧𝐚𝐥𝐈𝐠𝐧𝐨𝐫𝐞𝐂𝐚𝐬𝐞 parameter, compare strings using ordinal (binary) sort rules and ignoring the case of the strings being compared.