gbbb
gbbb

Reputation: 213

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?

Upvotes: 0

Views: 316

Answers (5)

Eric Lippert
Eric Lippert

Reputation: 660024

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.

Upvotes: 4

Peter
Peter

Reputation: 5728

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, i.e. 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"));

Upvotes: 1

WhileTrueSleep
WhileTrueSleep

Reputation: 1534

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

Upvotes: 1

Soner Gönül
Soner Gönül

Reputation: 98750

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

Upvotes: 1

Darren
Darren

Reputation: 70728

Remove .ToArray():

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

Upvotes: 5

Related Questions