-4

I named a variable private in C# and I get below error.

bool private = false;

Program.cs(15,12): error CS1001: Identifier expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj] Program.cs(15,12): error CS1002: ; expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj] Program.cs(15,12): error CS1513: } expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj] Program.cs(15,20): error CS1519: Invalid token '=' in class, struct, or interface member declaration [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj] Program.cs(21,1): error CS1022: Type or namespace definition, or end-of-file expected [/home/ccuser/workspace/csharp-data-types-variables-handling-errors-csharp/e7-workspace.csproj]

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69

2 Answers2

8

Yes, it is a reserved keyword. You can find a full list on the C# Keywords page in the docs.

If you really want to use private you can prefix it with @ (i.e. bool @private = false;) but you probably should just choose a different name, such as isPrivate.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • 1
    +1 for the `@`, didn't know about it. Is it only used for this need (creating variable with reserved keyword name)? – Daniel Sep 10 '19 at 05:58
  • 1
    @Daniel I believe so. Of course there's also the [verbatim identifier](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim) with strings which uses `@`, although it has a different meaning and usage there. – ProgrammingLlama Sep 10 '19 at 05:59
  • There's something else actually :) https://stackoverflow.com/a/429556/11397775 – Jojofoulk Sep 10 '19 at 05:59
1

You should use IDE, so you could see straight-away that something is a reserved keyword (alongside with indication of an error), like here in Visual Studio:

enter image description here

it colors blue all reserved keywords :)

You could pefix it with @ or _, choose any:

enter image description here

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69