Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I'm getting CS1012: "Too many characters in character literal" and CS0019?

Tags:

c#

When trying to upload something to Imgur, I have to put an Authorization in. I do it with WebRequest.Headers but it gives me three errors.

2 times CS1012 error

Too many characters in character literal

and 1 time CS0019 error:

Operator '+' cannot be applied to operands of type 'char' and 'method group'

This is the code:

webRequest.Headers['Authorization'] = 'Bearer ' + GetToken; 

What have I done wrong, how can I fix it, and how does it work? This is uploading with Imgur, I don't know if the 'GetToken' thing is right but it's to get the AccessToken, which should work correctly if I'm right.

like image 459
stepper Avatar asked Dec 15 '13 21:12

stepper


People also ask

What does too many characters in character literal?

A char can hold a single character only, a character literal is a single character in single quote, i.e. '&' - if you have more characters than one you want to use a string, for that you have to use double quotes: case "&&": Follow this answer to receive notifications.

What is meant by character literals?

A character literal contains a sequence of characters or escape sequences enclosed in single quotation mark symbols, for example 'c' . A character literal may be prefixed with the letter L, for example L'c' . A character literal without the L prefix is an ordinary character literal or a narrow character literal.


1 Answers

You're trying to use single quotes for string literals - that's invalid in C#. Single quotes are for character literals (char). You need double quotes for string literals. You also need parentheses for a method call:

webRequest.Headers["Authorization"] = "Bearer " + GetToken(); 

(Note that this has nothing to do with imgur or WebRequest - it's just normal C#.)

Links to MSDN explanations with samples:

  • CS1012 - "Too many characters in character literal"
  • CS0019 - "Operator 'operator' cannot be applied to operands of type 'type' and 'type'"
like image 60
Jon Skeet Avatar answered Sep 28 '22 04:09

Jon Skeet