Why is there no String.Toint() method?

Asked

Viewed 192 times

4

String in C# is an object, right? From what I saw to string is a class, different from int and other primitive types that are simple types, (I’ve seen on another site too, that they are all objects derived from System, then I do not say with total certainty). A string has some methods like String.Equals(), String.Compare(), etc, because there is no method String.ToInt() to replace the Int32.Parse()? If possible an explained of the types in general would be legal also.

2 answers

7


String in C# is an object, right?

All types are objects.

From what I saw string is a class, different from int and other primitive types that are simple types

String is a class, therefore a type by reference, but with type semantics by value. Int32 and other similar types are by value, they are structss. In C# do not have this idea of primitive type as in Java. string and int are nicknames for the type.

I’ve seen on another site also, that they are all objects derived from System, then I do not say with complete certainty

They are, everything ends up being derived from Object, String directly, and Int32 indirectly since it is derived from ValueType which is derived from Object.

System is a namespace, is just a surname for the type, has no inheritance relationship between them, only a way to name in a more organized way.

The string has some methods like String.Equals(), String.Compare(), etc., why there is no method String.ToInt() to replace the Int32.Parse()?

And why should I? It’s up to you to explain why that would be desirable. What would be gained from having a conversion within a string for a whole?

String is a universal type, can represent any type, although not all are easily representative. Every type can have a representation and it will always be through a text, even a simple number is shown to you as a character text(s).

Few types need a conversion of string for him. Many may receive a string to compose it, but not a conversion. Even if all types were to be precise, each would have a different rule of how to understand that text and know that it is valid and how it should be interpreted. How to put all this on the guy String?

If he has to be responsible for every conversion of his to other types, it’s unfeasible. In fact, conceptually if you think about it, no type should be responsible for any conversion to its type or to another type because there would be a huge amount of possible combinations, and types that even exist when you created a certain type would have to be added after without the creator of the new type having access to what you want to convert.

So the basic rule is that a new type must have an auxiliary type for conversion or must be responsible for the conversion of existing types. A type that can be considered universal would be the first of all.

The same perhaps could be said of Int32, it does not cease to be a type that exists right at the beginning of the language, but it does not communicate with all types in any way, so many types do not need to convert to it or it convert from it.

Numeric types need to be converted to each other, a more limited amount of conversions, so it is common to have the ability to do this, in general implementing the interface IConvertible.

Let’s say you create a numeric type of your own, what will you do to work well with other numeric types? Implement this interface in your type.

Your type knows what are the rules a text should have to be compatible with it. The type String don’t know, and you can’t touch the type String, and cannot inherit from him (this is another matter), but even if he could inherit, he should not inherit for this kind of thing. The solution is always to put the conversion of more universal types in its type.

The framework has chosen to follow the same line you need to follow, it is more intuitive so.

Even so it is possible to inject specific rules that make sense for that type, all numerical types allow it. If you did this in the type String would have to accept rules in general , could not limit, unless it had several versions of the conversion method that accept different rules. That guy would be the craziest thing you’ve ever done.

Any kind interact with String is easy to solve, the String interact with any type is difficult.

Note that we are talking about conversion, but at the bottom is an interpretation, it is not a Convert, is a Parse.

I take this opportunity to say that this method can only be used if you are sure that the interpretation will be successful, if you have a chance of being wrong you should use the TryParse(). You want me to go beyond Parse() for each type have a TryParse() also in the type String?

0

When defining an API, you have to be as generic as possible, mainly to avoid rework in the future. As there are already methods that do this routine, it is unnecessary to replace it in the String class. However, for practicality, you can create an extension:

public static class StringExtensions
{
  public static int ToInt(this string str)
  {
    return int.Parse(str);
  }
}

And use it like this:

int i = "1".ToInt();

Browser other questions tagged

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