DotNETWeekly-io / DotNetWeekly

DotNet weekly newsletter
MIT License
198 stars 3 forks source link

【文章推荐】6 条 C# 性能提升的建议 #654

Closed gaufung closed 1 month ago

gaufung commented 1 month ago

https://www.code4it.dev/blog/top-6-string-performance-tips/

gaufung commented 1 month ago
  1. StringBuilder Vs String concatenation

    • 如果只有几个字符串,使用字符串拼接
    • 如果有很多字符串,使用 StringBuilder
  2. EndsWith(string) Vs EndsWith(char)

EndsWith(char) 性能比较好

  1. IsNullOrEmpty Vs IsNullOrWHitespace Vs IsNullOrEmpty + Trim
  1. ToUpper Vs ToUpperInvariant 和 ToLower Vs ToLowerInvariant
  1. OrdinalIgnoreCase Vs InvariantCultureIgnoreCase
var s1 = = "Straße"; // German for "street"
var s2 ="STRASSE";

string.Equals(s1, s2, StringComparison.InvariantCultureIgnoreCase); //True
string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase); //False
  1. Newtonsoft Vs System.Text.Json

System.Text.Json 在时间和内存消耗上比 Newtonsoft 有更好的表现