[Home](../../) › [Metalama](../../metalama) › [Features](../features.md) › [Core Features](core.md) › Code Generation for C#

# Code Generation for C#

> This article presents different code generation techniques for C# and compares them to Metalama.

Canonical: https://postsharp.net/metalama/features/code-generation.html

Why write repetitive code by hand when you can generate it automatically? C# lets you name a method or a class, but it has no keyword for a recurring _pattern_, so the boilerplate must be written by hand. That is the gap Metalama fills. Metalama is the most capable code generator for .NET, thanks to its unique C#-to-C# template technology.

## Benefits

Four reasons to eliminate repetitive code with a code generator:

* **Boost development productivity.** Using Metalama can reduce hand-written code by 10–50%, enabling developers to focus on meaningful tasks. Architects and senior developers act as productivity _multipliers_ by minimizing repetitive and error-prone work for the rest of the team.

* **Reduce code complexity.** Separating technical details from business logic results in cleaner, more readable code. New team members can contribute effectively without being overwhelmed by low-level technical details.

* **Improve reliability.** Fewer lines of code mean fewer defects. Lower complexity also contributes. Additionally, Metalama simplifies the implementation of resilience features, further improving your app's reliability.

* **Reduce maintenance costs.** Metalama's ultimate advantage lies in reducing maintenance costs and extending the lifespan of the codebase. Since maintenance accounts for 55%–95% of a software system's costs, keeping complexity low is crucial for post-release team productivity.

## Features

Metalama is the only framework for .NET that offers all of the following:

| Feature | Description |
|---------|-------------|
| [C#-to-C# Templates](https://postsharp.net/metalama/features/templates) | Classic code generators are essentially text generators and don’t provide syntax highlighting, code completion, or error checking for generated code. Metalama is a true, strongly-typed object-oriented code generator featuring T#, a unique C#-to-C# template language. |
| [Enhance Hand-Written Code](https://postsharp.net/metalama/features/override) | Metalama is the only Roslyn-based tool that lets you add behaviors to existing hand-written code. Other C# generators let you _add new classes_ to a project or _extend partial classes_, but they don't allow injecting new behaviors into existing methods, properties, or fields. |
| [Reference Generated Code From Source Code](https://postsharp.net/metalama/features/reference-from-source-code) | With Metalama, your source code can reference generated code as if it were itself source code. This is a significant advancement over previous MSIL-based generation, where code additions were not visible from source code. |
| [Dependency Injection Support](https://postsharp.net/metalama/features/dependency-injection) | You can use dependency injection concepts in your aspects to make both the aspects and the target code more testable. |
| [Flexible Code Selection Mechanisms](https://postsharp.net/metalama/features/applying) | Metalama offers several mechanisms to add aspects (which encapsulate code generation and validation) to target code: custom attributes for selective application, and fabrics for a bulk, programmatic approach. Also, explore cross-project options. |

---

## Site navigation

- Next: [Code Validation](code-validation.md)
- Index of the whole site: [llms.txt](../../llms.txt)

