Why are some string methods static?

Asked

Viewed 107 times

8

I am 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 in other languages this way

"123".Concat("456")

Why can’t you?

There’s a way to do it this way?

2 answers

5


The exact reason only the people who developed know, there are some versions.

Some say

  • Visual Studio didn’t handle Intellisense well when the literal string was the object. It seems to me very unlikely, to solve this would be a matter of hours or days. They wouldn’t make the library worse because of such nonsense. The problem really existed, but they wouldn’t change the syntax that is something definitive by such nonsense.

  • is because he is manipulating the two texts to generate a new one and not manipulating one of them. It seems sensible, but there all methods of string should be static because the string is immutable and he never manipulates the object differently. But it is a good theory.

  • did what Java did. In the rush to launch on time it may have actually occurred. I don’t know if it’s just that, but they may have adopted what worked so they didn’t spend more time thinking about it. But I still think they did, and the choice was for the previous reason.

  • the static method allows concatenating 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 designed to use null variables, obviously that it would make no sense to have the method one way for the literal and another for the variable or expression. Perhaps along with the second hypothesis is the most plausible reason. But with the advent of nullable References this will be less used, if they existed from the beginning it would make less sense to have this form.

  • to make it more comfortable for those who come from C or C++, in addition to Java, but I find it unlikely. They had no doubts in changing other things. At most, they considered it a bonus.

Language design is something extremely complex, the amount of possible combinations is incredibly large and it’s easy to forget some details. I may have forgotten some, or even been unaware of some of the language specification that required something like this, not because of this mechanism itself, but for something unrelated that is not orthogonal.

Solution

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 will not have a null there, which makes it not the most ideal solution if you use it a lot (the tendency is to use almost never with the nullable References that now exists in the language), and must include the 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 the type string, but it would be harder to use. What is the difference of 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) => string.Concat(str1, str2);
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

0

As we know, static methods, unlike those called by objects, 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 static method Concat(), you are simply free of a call by an object.

We can’t help but notice that the strange thing is the existence of nonstatic methods, because strings are immutable reference objects.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.