Why is it bad to provide an implicit String (char []) operator?

We can:

var t=new String(new[] { '็น', '้ซ”', 'ไธญ', 'ๆ–‡' }); 

or

 var encoding=Encoding.Unicode; var bytes=encoding.GetBytes(new[] { 'E', 'n', 'g', 'l', 'i', 's', 'h' }); var t=encoding.GetString(bytes); 

No comments:

 public static implicit operator String(char[] charArray) { return new String(charArray); } 

We can not:

 String t=new[] { '', '', '', '', '', '', '', '\x20', '', '', '', '' }; 

I know that the character array does not match the string ; but sometimes I just want to assign an array of characters to a string directly; without explicit casting, conversion, or new X(new Y(new Z ...

And I think, personally, the reason why it was not submitted, perhaps because:
The C-Sharp team wants programmers, especially those with experience with C ++ / C, to remember that C-Sharp is NOT the same as C ++ or C.

The question is why not? This is bad?

+4
source share
6 answers

Implicit conversions are a compiler function. There is nothing in the CLI specification that allows them; all conversions to IL are explicit. Even simple ones like int to long and swim to double. So your syntax requires a C # command in your case.

The way the C # team thinks about it is well published. Each possible function begins with -100 points and requires some serious motivation to get to +100 in order to justify the work associated with the design of the function, its implementation, documentation and its preservation. I canโ€™t speak for them, but I seriously doubt that this makes it past 0. The alternative is obvious and simple, so itโ€™s just not worth it.

+4
source

I do not know any specific documentation why they did not enable this feature.

I assume that they have a finite number of functions that they can implement with each version of .NET, and in the end they decided that the number of people who would find this useful function would not justify spending time (especially considering that there is a constructor which does exactly what you want).

I found this question and, although it does not directly answer your question, it does enter some important differences between char arrays and strings in .NET. In .NET, a string is not the same as an array of characters (as it can be in C ++). Perhaps they tried to hammer this concept at home.

+1
source

You can always have something like:

 char[] chars = { 'a','b','c','d' }; string s = new string(chars); 
0
source

A character array does not match a string, so you cannot assign them directly. That seems like a good reason to me.

I would do something like this:

 char[] arr = { 'a', 'b', 'c' }; string s = new String(arr); 
0
source

In the base class library, it will be difficult for you to find any example of an implicit (or explicit) conversion that changes the view, which is not mainly related to value types (now I can only think of numerical conversions between byte , short , int , long , float , double and signed / unsigned options).

As for why this conversion was not implemented ...

  • An array of characters and a string are very different in their behavior;
  • The loss of readability / comprehensibility is higher than the gain in the convenience of developers;
    • Enhanced developer convenience: less than 12 characters to enter;
    • Loss of readability:
      • The reader should know:
      • That there is a conversion
      • Is the target variable string or char[] ;
      • That the conversion creates and selects a new string object;
0
source

How should the compiler understand which type you are using?

 void DoIt(string data) { } void DoIt(char[] data) { } 

which of these methods should be called?

 DoIt(new[] { '', '', '', '', '', '', '', ' ', '', '', '', '' }) 

leaving this functionality just for the purpose, a poorly designed syntactic sugar will look like.

0
source

All Articles