C# : String
Top 37 C# Example Pages...

["ryws. XBXCEST~~}T~~YF64666666F((.BXBB(XP88A(CbB.S}T~~}T~~","Empty Strings."," An empty string has zero characters. These strings can be checked using a clear and efficient method. Developers frequently need to see if a string contains no characters. Microsoft's FxCop has one recommendation. ","FxCop ","fxcop","Example."," Here we see the five empty string tests. Each of the five tests will be true, as the string \"str\" is empty. The code prints out the results of the tests, which you can see at the end. ","Result: ","Strings were compared against empty strings. Checking Length was the fastest.","String Length ","string-length","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Based on:"," .NET 4.5\n\n","C# program that uses empty strings","\n\nclass Program\n{\n static void Main()\n {","\n // Example string.\n ","string str = ","\"\"",";","\n\n // Example tests.\n ","if (str == \"\")\n {","\n // 1\n // Test with op_Equality\n ","System.Console.WriteLine(1);\n }\n if (string.Equals(str, \"\"))\n {","\n // 2\n // Test with static Equals method\n ","System.Console.WriteLine(2);\n }\n if (string.IsNullOrEmpty(str))\n {","\n // 3\n // Test with IsNullOrEmpty\n ","System.Console.WriteLine(3);\n }\n if (str.Length == 0)\n {","\n // 4\n // Test with Length property\n ","System.Console.WriteLine(4);\n }\n if (str.Equals(\"\"))\n {","\n // 5\n // Test with Equals method\n ","System.Console.WriteLine(5);\n }\n }\n}\n\n","Output","\n\n1\n2\n3\n4\n5","First example."," This code shows the equality operator. For many developers, this is the clearest code, but it is not the fastest. But it won't cause severe performance problems. If you look at == in MSIL, it is called op_Equality.","Second example."," This part shows the static Equals method. This has the same semantics as part 1 but uses different syntax. I haven't seen this method used as often. It is an alternate syntax form.","Third example."," IsNullOrEmpty is a newer static method that wasn't present in the first releases of the .NET Framework. It performs well and I have seen it often in the wild. More details are available on IsNullOrEmpty. ","IsNullOrEmpty ","isnullorempty","Fourth example."," This shows the simple Length check. FxCop, Microsoft's static analysis tool, recommends this method. However, it throws a NullReferenceException when the string is null. This condition occurs often in programs. ","Null Strings ","null-string","NullReferenceException ","nullreferenceexception","Fifth example."," This part demonstrates the instance Equals method. This calling pattern is different because the compiler must know the string \"s\" is not null before calling the method. This changes the performance.","Benchmark."," I ran these if-statements through 100 million iterations each. I did this in an effort to improve an ASP.NET program that had to use these tests frequently, although I didn't measure any improvement in it. ","Benchmark results","\n\nEquality operator ==: "," 796 ms","\nstring.Equals: "," 811 ms","\nstring.IsNullOrEmpty: "," 312 ms","\nLength: "," 140 ms"," [fastest]\nInstance Equals: ","1077 ms","The fastest way"," is to access the Length property. If you need to detect null, consider IsNullOrEmpty. If your program uses a slow method to do this, and it runs thousands of times very frequently, improving this is worthwhile. ","Note: ","Microsoft's FxCop warns when you use the other methods. It is right when it states that Length is the fastest method.","String.Empty."," In the string class, there is a public readonly field called Empty. You can access this field by typing string.Empty, but this field has some negatives and I prefer the constant itself. ","string.Empty ","string-empty","Summary."," An empty string can be checked in many ways. We choose which one depending on program context and preferences. We saw five different ways. Observing benchmarks of this simple operation is important and useful. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANIAAAA5CAAAAABmcSrfAAAC8ElEQVR4Xu3Z263jMAwE0KhRtsBi2MrUwh5YhfYGizw8pkPZhu+uA8+fBSHIAfWyfAsfSPR9cZP2EzXv90Txw/tygwwEezyB9oreUfp89FOS0KaxHu3UpNDGMZya5C3LmUnevo3k7dtI0b6OpP+IdHfZO8AfWuwkoX0bKRpHAT01CQSyoH3qdKRg0YMqBckBVTUMnv/CAXhsJJnM47PWvEjK1pzk70UUxKfCS+8BeTxYDJAcz/h6Es2a6M/YIsmlUSyWSQ0u1LcgUVaSgov0ii+QQlsSpKQ8fijJl/9YJxKViGPjpIYjSZaPOx6TPnB4snFS8wNJ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JkECfJIvKUsUf2ReB4Z2VIwpkIA+r9EJ4wBG6oPEGBmGU5b5CPThswTEoIAAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAG4AAACCAgMAAADasxWRAAAADFBMVEX/6Nn///+VlZX29va5ksB5AAAASElEQVR4AWPACZj/f0DmjkqOSjKtQgcLyJccZiEE9SOmP8mXHEaBMBoIo4EwGggryJccvvVKKAogTnJUclRyVHLklgmjkqOSAALWyrsxEHLDAAAAAElFTkSuQmCC)"]

["C","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABBCAMAAABYbu5jAAAAMFBMVEX///+1wv+jqv9siv/p6//W3f8zZv9gb//19v9NK/8zAP999//S//8z//8zzP+z6P/0gInFAAABg0lEQVR4XtXXy1ICURAD0H7c96D+/9/KQqkhlEKD0iTrWZzKYtJXbou2s+jvn1Q55f2mfAMOb6Ec5D6+c/ObcvOdm98qN9+5+a1w842b3wo33/6Dv2H22g3zCL+PKF/uSKDsGL8pN9+5+U25+c7Nb5Wb74R8h+ki41eYLja+Qf1k/AL1k/HF4XIg4ytMFxl/X7/z8aF+Ov6A6bqR//FTnswXa2B96LmyPZtfoH4yvsDlwMaHy4GLj/Xz8RXqJ+PD5UDGh+ni448O08XEh+lSrtUVnC6qm+ey/krGx+li4+N0sfHh7Cfj43Sx8bF+Mj5OFxkf67c8frF+jJUovzRIBr/4+kovMb5YPr/OdcrUGL9k8FG/Tw3xxbP55/o1R4hfk/m6IBbii+fyJ/JnjK+5/HWRGuJLfzG+xvh6lX89f8m3GH90ar7Ya/FrkF8y+XNhRpAvlsi3+H8fUxP5A+svYb54Hl8Uy4/zNZEvCvo4XzyRLzrhXg7zNZMvw/o6ptsQpvDnE+wJyVGQ5SG7AAAAAElFTkSuQmCC)","url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANYAAAA3CAAAAABVkOvVAAABSUlEQVR4Xu3Y4WnEMAyGYW+sfbSBlvh20Bbawq3TF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