4

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);

Without sometning like:

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

We cannot:

String t=new[] { 'р', 'у', 'с', 'с', 'к', 'и', 'й', '\x20', 'я', 'з', 'ы', 'к' };

I know that character array is not the same as a string; but sometimes I just want to assign a character array to a string directly; without explicit casting, converting or new X(new Y(new Z ....

And I think, personally, the reason it's not provided is possibly because:
The C-Sharp team wants the programmers, especially who have experience of C++/C to keep in mind that C-Sharp is NOT as similar as C++ or C.

The question is WHY not? Is that bad?

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
  • I really don't know what you are looking for. An assignment moves the value of one variable to another, or moves a reference from one variable to another. How can this work for different types if there is no conversion? That's what I addressed in my answer. I'm not sure what else you want to hear. – Jonathan Wood Jan 11 '13 at 17:49
  • @JonathanWood As `string` implements `IEnumerable`, maybe he expected an implicit cast between `string` and `char[]`. – ken2k Jan 11 '13 at 17:51
  • I'm not looking for. If this was implemented, in fact, it calls the existing constructor. I'd like to know why not allowing that syntax by an implicit operator. – Ken Kin Jan 11 '13 at 18:08

6 Answers6

4

Implicit conversions are a compiler feature. There's nothing in the CLI spec that permits them, all conversions in IL are explicit. Even the simple ones like int to long and float to double. So it is up to the C# team in your case to make that syntax work.

The way the C# team thinks about that is well published. Every possible feature starts at -100 points and needs some serious motivation to get to +100 to justify the work involved with designing the feature, implementing it, documenting it and maintaining it. I can't speak for them, but I seriously doubt this one makes it past 0. The alternative is obvious and simple so it just isn't worth it.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • +1, Could you point us to where the C# team talks about the 1000 point thing? – Abe Miessler Jan 11 '13 at 17:59
  • 1
    @Abe: It is minus 100 points http://blogs.msdn.com/b/ericgu/archive/2004/01/12/57985.aspx I also strongly suspect that that feature ends at even less than minus 100. – Jean Hominal Jan 11 '13 at 18:00
1

I don't know of any specific documentation out there for why they did not include this functionality.

My guess would be that they have a finite number of features they can implement with every release of .NET and at the end of the day they decided that the number of people that would find this feature beneficial did not justify spending the time to implement it (especially considering that there is a constructor that does exactly what you want).

I found this question and while it doesn't directly answer you question it does go into some of the important differences between char arrays and strings in .NET. In .NET a string is not the same thing as an array of chars (as it might be in C++). Perhaps they were trying to hammer this concept home.

Community
  • 1
  • 1
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
0

You can always have something like :

char[] chars = { 'a','b','c','d' };
string s = new string(chars);
prthrokz
  • 1,120
  • 8
  • 16
0

A character array is not the same as a string, so you can't assign them directly. That seems like as good a reason as any to me.

I'd do something like this:

char[] arr = { 'a', 'b', 'c' };
string s = new String(arr);
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
0

In the base class library, you would be hard pressed to find any example of an implicit (or explicit) representation-changing conversion that did not involve mainly value types (right now I can only think of the number conversions between byte, short, int, long, float, double and the signed/unsigned variants).

As for why that conversion was not implemented...

  • An array of characters and a string are very different in behaviour;
  • The loss in readability/understandability is higher than the gain in developer convenience;
    • Gain in developer convenience: 12 less characters to type;
    • Loss in readability:
      • The reader has to know:
      • That the conversion exists;
      • Whether the target variable is a string or a char[];
      • That the conversion creates and allocates a new string object;
Jean Hominal
  • 16,518
  • 5
  • 56
  • 90
  • Compare to `var t=new String(new []{ ...` syntax, `String t=new[]{...` will not reduce much characters to type. But in the syntax I specified, the type of `String` should be explicit declared, or the implicit operator is not invoked. – Ken Kin Jan 11 '13 at 18:43
  • 1
    @KenKin: But you can have implicit conversions kick off at other places, such as when you pass an argument into a function, or when you assign a variable after its type has been defined. Besides, there is already a lot of syntax support for string, I do not think I have used the constructor from a char array more than twice after developing on .NET for 2 years and a half. – Jean Hominal Jan 13 '13 at 12:14
0

How compiler should understand what type exact you use?

void DoIt(string data)
{
}

void DoIt(char[] data)
{
}

which of these methods should be called?

DoIt(new[] { 'р', 'у', 'с', 'с', 'к', 'и', 'й', ' ', 'я', 'з', 'ы', 'к' })

leaving this functionality just for assign would look like badly designed syntax sugar.

Alexander Balte
  • 900
  • 5
  • 11
  • Compiler would not be confused by the implicit operator. If you declared `String t=new []{ ...` and call `DoIt(t)`, you are calling the overload of `String`. If you call `Doit(new []{ 'p', ... })`, then you are not. – Ken Kin Jan 11 '13 at 18:50