-1

I renamed a class with name static, i have compile time error

public class static
    {

    }

Error CS0116 A namespace cannot directly contain members such as fields or

John Doe
  • 141
  • 1
  • 2
  • 10
  • 7
    Use escape `@static` to use keyword as identifier (but this is not recommended, use other class name). – Tetsuya Yamamoto Dec 14 '17 at 10:06
  • 1
    why the hell vote down my question, what is wrong in this question, this is not fair – John Doe Dec 14 '17 at 10:06
  • https://stackoverflow.com/questions/2787716/use-the-long-reserved-word-as-a-variable-name-in-c-sharp you will find this useful I believe. – kuskmen Dec 14 '17 at 10:10
  • 5
    If you like this name, use `public class Static {}`. As a bonus that will also follow standard C# conventions for naming classes (first letter is upper-case). – Evk Dec 14 '17 at 10:12
  • @Evk Great Idea. – John Doe Dec 14 '17 at 10:13
  • 2
    "this is not fair"...well it's a matter of opinion (and I didn't downvote) but if you hover over the vote button you'll see it can be given because "this question does not show any research effort, it is unclear or not useful". Personally I'd have thought you could probably have googled the error message and found the reason for it at least, and possibly the solution. So maybe the downvoter thought you could have done more research, I don't know. – ADyson Dec 14 '17 at 10:15
  • @ADyson Everyone having good reputations must appreciate beginners, i have shown my effort in the question, i always try my best to ask question with effort. thanks – John Doe Dec 14 '17 at 10:16
  • 2
    Where's your effort? You've reported the error, verbatim, and nothing more. There's no evidence of any attempt to explain or fix it. Even for a beginner, you can still look for existing information online or in books. I I appreciate you don't always know exactly what to type when searching but it's a process of trial and error sometimes. Anyway the answer is now below, so it's kind of moot. I just wanted to help you understand why people vote up and down. – ADyson Dec 14 '17 at 10:17
  • 2
    @JohnDoe But the thing is if you would have googled once how to escape signal words (its the same e.g. with declaring a filepath with "`/`") that you need to escape this signal sequence somehow and its always done with `@`. And that is what we are talking about, just *some effort*. There a lots of questions out there about this specific topic. :-) – L. Guthardt Dec 14 '17 at 10:18
  • OK i get your idea. – John Doe Dec 14 '17 at 10:18
  • 1
    Downvote, because it's a duplicate. And shows no research effort. – Tobias Theel Dec 14 '17 at 10:19
  • Possible duplicate of [Use the long reserved word as a variable name in C#](https://stackoverflow.com/questions/2787716/use-the-long-reserved-word-as-a-variable-name-in-c-sharp) – Tobias Theel Dec 14 '17 at 10:20
  • 1
    Possible duplicate of [how to escape key words in c#?](https://stackoverflow.com/questions/2588244/how-to-escape-key-words-in-c) – L. Guthardt Dec 14 '17 at 10:21
  • How did this question get these few upvotes? It's one of *the* most basic stuff in C# that **has to be** looked up on your own. If this is already a *good question* then every question cant be declared as *good*. – L. Guthardt Dec 14 '17 at 10:27
  • 1
    If i might return to the question for a moment :-), the suggestion to use `public class Static {}` is, I think, the best answer to the actual question (and preferable to `@static`), but I don't think that naming classes with the same name as a reserved word is a great idea - it can get confusing. Furthermore, `Static` does not feel very descriptive. Class names tend to be nouns, describing the thing they represent. Is there simply a different name you could use? – bornfromanegg Dec 14 '17 at 10:41

2 Answers2

4

it's really a bad idea to use reserved keyword as a class name, but here is the way to do it

public class @static
{
    ....
}

//create a instance of class static
var temp = new @static();
Evan Huang
  • 1,245
  • 9
  • 16
1

Short answer you dont

Keywords are predefined, reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a valid identifier, but if is not because if is a keyword. The first table in this topic lists keywords that are reserved identifiers in any part of a C# program. The second table in this topic lists the contextual keywords in C#. Contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context. Generally, as new keywords are added to the C# language, they are added as contextual keywords in order to avoid breaking programs written in earlier versions.

you can find more info here

C# Keywords

however you can as pointed out in the comments use the prefixed @ symbol, however this is serious code smell

TheGeneral
  • 79,002
  • 9
  • 103
  • 141