简体   繁体   中英

C# string storage for white-space

I'm asking if there's a difference between the following two strings:

string s1 = "Hello World";
string s2 = "Hello" + " " + "World";

because "Hello" + " " + "World" is working for me with List functions like .Any() .Contains() and .Equals() whereas "Hello World" doesn't work for these functions here.

the simple answer is that there is no difference!

The IEnumerable extension methods (also called LINQ for objects) you mentioned (.Any() etc.) work on both strings as string implements IEnumerable<char>

example:

string s1 = "Hello World";
string s2 = "Hello" + " " + "World";

Console.WriteLine(string.Concat(s1.Select(s => s.ToString())));
Console.WriteLine(string.Concat(s2.Select(s => s.ToString())));

output:

Hello World
Hello World

try it: https://dotnetfiddle.net/BKTMHj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM