Tag: 为空

为什么String.IsNullOrEmpty比String.Length快?

ILSpy显示String.IsNullOrEmpty是根据String.Length 。 但是为什么String.IsNullOrEmpty(s)比s.Length == 0更快? 例如,它在此基准测试中的速度提高了5%: var stopwatches = Enumerable.Range(0, 4).Select(_ => new Stopwatch()).ToArray(); var strings = “A,B,,C,DE,F,,G,H,,,,I,J,,K,L,MN,OP,Q,R,STU,V,W,X,Y,Z,”.Split(‘,’); var testers = new Func[] { s => s == String.Empty, s => s.Length == 0, s => String.IsNullOrEmpty(s), s => s == “” }; int count = 0; for (int i = 0; i < 10000; […]