The exact reason only the people you developed know, there are some versions.
Some people say that
-
Visual Studio did not handle Intellisense well when the string literal was the object. It seems very improbable to me, to solve this would be a question of hours or days. They would not make the library worse because of such nonsense. The problem really existed, but they would not change the syntax that is definitive something for such nonsense.
-
It is because it is manipulating the two texts to generate a new one and not manipulating one of them. It seems sensible, but then all string
methods should be static because the string is immutable and it never manipulates the object differently. But it's a good theory.
-
have done what Java did. In the rush to launch on time may have actually occurred. I do not know if it's just that, but they may have taken what already worked so they do not spend any more time thinking about it. But I still think that they thought and the choice for the previous reason.
-
The static method allows you to concatenate a null initially, which would be impossible with the instance method, and this behavior is desirable. Of course, no one will concatenate a null literal, but this was meant to use null variables, obviously it would not make sense to have the method one way for the literal and another for the variable or expression. Perhaps together with the second hypothesis is the most plausible reason.
-
to make it more comfortable for people coming from C or C ++, in addition to Java, but I find it unlikely. They had no doubts about changing other things. At most, they thought this was a bonus.
Language design is something extremely complex, the amount of possible combinations is incredibly large and it is easy to forget some details. I may have forgotten some, or even know something of the language specification that required something like this, not because of this feature itself, but because of something unrelated that is not orthogonal.
I think the best way to solve this is by creating an extension method. But besides it does not work with a null generating an exception, so you need to make sure that you do not have a null there, which makes it not the ideal solution, and must include System
for the extension method to be called. Or you need another namespace if you do not want the extension method to be always available for type string
, but it would be more difficult to use. What's the difference between String vs String?
using System;
using static System.Console;
public class Program {
public static void Main() {
WriteLine("123".Concatenate("456"));
string.Concat(null, "xxx");
}
}
namespace System { //isto estaria provavelmente em outro arquivo ou até outro projeto, talvez junto com outros métodos de extensão.
public static class StringExt {
public static string Concatenate(this string str1, string str2) {
return string.Concat(str1, str2);
}
}
}
See running on .NET Fiddle . And at Coding Ground . Also put it on GitHub for future reference .