1

In my domain model I have an Event entity. This means that I have to sometimes declare variables as: @event since event is a reserved key word.

I've read on a few stack overflow posts (like What's the use/meaning of the @ character in variable names in C#?) that this is not recommended unless you're interacting with other programming languages. My question is why is it not recommended? What is the issue with using @?

I could use an "Occasion" entity instead but that would mean in my UI layer I would have events which maps to occasions?

Community
  • 1
  • 1
Cool Breeze
  • 1,289
  • 11
  • 34
  • 1
    Please add references to the `few stack overflow posts` that call the practice into question. My personal take is that it would cause confusion and leave room for errors. – Eric J. Dec 18 '14 at 01:45
  • 2
    There is nothing technically wrong with `@variableName`, so I don't think it is possible to give good concrete non-opinion based answer to this question... – Alexei Levenkov Dec 18 '14 at 01:45
  • @AlexeiLevenkov If that is the case then I think i can justify using it in this particular scenario. I just wanted to make sure i wasn't missing something. – Cool Breeze Dec 18 '14 at 01:58
  • @EricJ. I see your point but it would also cause confusion mapping between an Occasion entity to an EventDTO. – Cool Breeze Dec 18 '14 at 02:00
  • Just be careful to not confuse everyone... Something like `dynamic dynamic = 4;` or `var @if = true; if(@if)...` is valid C#, but not necessary immediately readable. – Alexei Levenkov Dec 18 '14 at 02:06
  • haha that gives me a headache just looking at it. The only time it is an issue is when I have an event entity as a parameter. Ends up being MethodName(Event @event); – Cool Breeze Dec 18 '14 at 02:09

1 Answers1

2

What you're trying to do is the entire purpose of the @ prefix to prevent name clashes.

But this text from MSDN says everything you're asking:

The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

Source

So it just comes down to style, in your case it's the right solution if you don't want to rename your entity.

Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
prospector
  • 3,389
  • 1
  • 23
  • 40