add tag
Hosch250
My program is erroring with the error

>CS0029: Cannot implicitly convert type 'char' to 'string'

My code is:

```
string str = 'c';
```

What is going wrong and how can I fix this?
Top Answer
Hosch250
This error is because `'c'` is a character (`char`) literal in C#, not a `string` literal, and there is no implicit conversion defined between them. Now you might be wondering what an implicit conversion is, and why "implicit" was specified.

An implicit conversion is one the compiler can run without a hint from the user. An example is:

```
decimal d = 3;
```

The compiler parses `3` as an integer (`int`) literal, but it can convert it to the `decimal` variable type without us explicitly telling it to with a cast. If we were to write this with an explicit cast, it would look like:

```
decimal d = (decimal) 3;
```

Another common place you might see this error is when returning a value of one type from a function that declares a different return type:
```
public string GetString() => 'c';
```

To solve this specific problem, you can use one of the following in various cases:
```
// we control the value and can use a string literal
string str = "c";

// we have a value coming from somewhere else and can't just change it to a string literal
char c = 'c';
string str = $"{c}";                    // string interpolation
string str = string.Format("{0}", c);   // string interpolation compiles to this behind the scenes
string str = new string(c, 1);          // declare a new string from the character and specify the character repeats once
string str = new string(new[] { c });   // initialize a string from an array of characters
```

Now, this terminology is useful, but how does the compiler know when it can convert between two values automatically vs requiring an explicit cast, or if it can do it at all? It's based on whether a cast operator is defined, and if it is, whether it is defined with the `implicit` or `explicit` keyword.

An explicit cast is defined as:

```
public static explicit operator Type1(Type2 d) => {instance of Type1};
```

An implicit cast is defined the same way, but with the `implicit` keyword instead of `explicit`. It can be used as an explicit cast as well, but there is no need; the compiler can just use it without being told to if the usage is known to be the output type.

So, this error is telling you that in the .NET libraries, there is no operator defined as

```
public static implicit operator string(char c);
```

There isn't an explicit operator defined either in this case; if you tried using an explicit cast, you'd get the following error instead:
> CS0030: Cannot convert type 'char' to 'string'

If there had been an explicit operator defined, the error would let you know:
> CS0266: Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

If you were to implicitly implement a cast operator, you should guarantee that no information is lost and there is no path that leads to an exception. This would be easy enough to do with `char` to `string` as a `string` is just an array of `char`s, and there is no data list with an `int` to `decimal` conversion, as in my example above. However, you should, for example, not implement an implicit cast from `decimal` to `int` because it would lose the floating point part of the number; that cast should be (and is) an explicit cast.

## References

1: [Error 0029](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0029).  
2: [Error 0030](https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0030).  
3: [Cast expressions](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/type-testing-and-cast#cast-expression).  
4: [Implementing a user-defined cast operator](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/user-defined-conversion-operators).

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.