How do I use a C keyword as a property name?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In C#, keywords such as class, event, and namespace are reserved, so using them as property names can trigger compile errors. This becomes common when your model must match an external JSON contract that you cannot rename. The best approach is usually to keep internal code readable and use serializer mapping for wire compatibility.
Option 1: Escaped Identifiers with @
C# lets you use a keyword as an identifier by prefixing it with @. This is syntactic escaping in source code only.
This is the smallest fix, especially for generated code where exact field names matter.
Option 2: Readable Internal Names with JSON Mapping
For application code, readable names are easier to maintain. Use attributes to map internal names to external keyword fields.
This keeps domain language clear while preserving API compatibility.
Newtonsoft.Json Equivalent
If your project still uses Newtonsoft.Json, use JsonProperty instead of JsonPropertyName.
Avoid mixing serializer attributes from both libraries in one model unless migration rules are clearly documented.
Runtime Reflection Behavior
Escaping with @ does not change the runtime member name in metadata. Reflection sees the logical identifier without @.
That is why explicit serializer attributes are still important when external naming differs from project conventions.
Recommended Team Conventions
A consistent naming policy prevents confusion in code reviews and onboarding.
- Generated transport models may keep escaped keyword names.
- Handwritten domain models should prefer readable names.
- Mappings should be explicit with serializer attributes.
- Contract tests should validate serialization and deserialization.
This separation makes code easier to reason about while keeping protocol fidelity.
Migration Strategy for Existing Code
If your codebase has mixed styles, migrate incrementally:
- Identify keyword-like names across DTO classes.
- Decide which classes are transport-only and which are domain-facing.
- Apply mapping attributes for domain-facing classes.
- Add unit tests for expected JSON field names.
Example serialization test:
Testing contract shape directly catches breaking changes early.
Common Pitfalls
- Forgetting the
@prefix on one usage and getting inconsistent compile errors. - Assuming escaped identifiers automatically control JSON field names.
- Exposing keyword-heavy naming across internal domain logic.
- Mixing
System.Text.Jsonand Newtonsoft attributes without a migration plan. - Skipping contract tests after serializer or naming policy changes.
Summary
- C# supports keyword identifiers via the
@escape syntax. - For maintainability, prefer readable property names plus explicit JSON mapping.
- Escaped syntax affects source code, not runtime metadata naming.
- Keep a clear boundary between generated transport models and domain models.
- Add contract tests to protect wire-format compatibility during refactors.
Related reading
- How do I use OpenFileDialog to select a folder?
- How do I use reflection to determine the nested type element type of an array?
- How do I use WPF bindings with RelativeSource?
- How do I verify a method was called exactly once with Moq?
- How do I view the SQL generated by the Entity Framework?
- how do I work around log4net keeping changing publickeytoken
- How do I write LINQ's .Skip1000.Take100 in pure SQL?
- How do I write one to many query in Dapper.Net?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.