Why are some string methods static?

8

I'm learning C # after already working with other languages, I noticed that some methods need to be called by the class

string.Concat("123","456")

I usually use it in other languages this way

"123".Concat("456")

Why can not you?

Can you do it this way?

    
asked by anonymous 13.03.2017 / 13:29

2 answers

5

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 .

    
13.03.2017 / 13:47
0

As we know, static methods, unlike objects called, are independent of these instances.

In the example "testo".Replace('s','x') there is a value to be modified, that is, it is very convenient to call the method by the object.

In the case of the Concat () static method, you are simply free of a call by an object.

We can not help but notice that the strange thing is the existence of non-static methods, because strings are immutable reference objects.

    
13.03.2017 / 15:11