简体   繁体   中英

Implicit conversion from type Char[] to string is not possible

I want to do the following but I get this

Error: an Implicit conversion from type Char[] to string is not possible.

string Pattern2 = (Convert.ToDateTime(currMail.CreationTime).ToString(" dd-MMM-yyyy HH-mm")).ToArray();

Does anybody have any idea as on how to deal with this?

删除.ToArray()

string Pattern2 = Convert.ToDateTime(currMail.CreationTime).ToString("dd-MMM-yyyy HH-mm");

As the other answers point out, your call to ToArray is not just unnecessary, it is in this case actively harmful. You already have a string in hand, you need a string, so don't convert the string to an array of char; just use the string.

However, for your future reference it is possible to convert an array of char to a string, just not via an implicit or explicit conversion. The syntax for that is:

char[] characters = whatever;
string str = new String(characters);

Finally, the documentation is here:

http://msdn.microsoft.com/en-us/library/vstudio/s1wwdcbf.aspx

Beginners should familiarize themselves with this documentation; there's a lot of good stuff in there.

Looks like you don't need to use .ToArray() method at all. You already using .ToString() method for assigning to your Pattern2 variable.

Just use as;

string Pattern2 = Convert.ToDateTime(currMail.CreationTime).ToString("dd-MMM-yyyy HH-mm");
 string Pattern2 = Convert.ToDateTime(currMail.CreationTime).ToString(" dd-MMM-yyyy HH-mm");

You assign a char[] to a string, which requires conversion from the char[] to a string. As the error says, this is not done implicitly, ie behind the scenes. This is done to prevent silly mistakes.

You are expected to make an explicit conversion (create a string from the array and then assign it).

In your case you have a string and convert it to an array before assigning it to Pattern2. Just don't convert the string to an array.

string Pattern2 = (Convert.ToDateTime(currMail.CreationTime).ToString(" dd-MMM-yyyy HH-mm"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM