문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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.

You can always have something like :

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

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

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;

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top