# Namespace and Using

Namespaces are critical for writing robust, maintainable C# code. They organize your classes, prevent naming conflicts, and enhance development productivity by providing context for your types. Whether you’re building a game in Unity or any C# project, always defining your code within a namespace is a best practice.

### Why Use Namespaces?

Namespaces provide a layer of disambiguation. Without them, common class names like `Input`, `Player`, or `CameraController` could clash with those from other libraries or frameworks (for example, Unity’s built-in types). Consider this example:

```csharp
public class Input
{
    // Your custom input implementation
}
```

Without a namespace, this class might conflict with `UnityEngine.Input` in any MonoBehaviour, leading to hard-to-debug errors. Proper namespacing ensures that each class has a unique, context-rich identifier.

#### Benefits of Using Namespaces

* **Disambiguation:** Separates your code from similar class names in external libraries.
* **Organization:** Groups related classes, improving project structure and maintainability.
* **IDE Support:** Enhances features like autocomplete, refactoring, and navigation.
* **Alias Flexibility:** Lets you simplify long or nested namespace paths via aliasing.

### The `using` Directive

The `using` directive allows you to reference types without their fully qualified names. This reduces verbosity and improves readability, especially when dealing with deeply nested namespaces.

#### Basic Usage

Instead of always writing out full namespace paths like:

```csharp
UnityEngine.Input;
HeathenEngineering.SteamworksIntegration.API.Input;
```

You can include these at the top of your file:

```csharp
using UnityEngine;
using HeathenEngineering.SteamworksIntegration.API;
```

Now you can simply refer to `Input`, but this can lead to ambiguity if both namespaces define a type called `Input`.

#### Resolving Ambiguity with Aliasing

To prevent conflicts, you can alias namespaces:

```csharp
using Unity = UnityEngine;
using API = HeathenEngineering.SteamworksIntegration.API;
```

This way, you can differentiate between:

```csharp
// Unity's Input class:
Unity.Input;

// Steam API's Input class:
API.Input;
```

#### Advanced Aliasing with Static Classes

Sometimes, you might want to simplify access to nested static classes. For example, if the Steam API’s `Input` class contains nested client functionality, you can alias it:

```csharp
using SteamInput = HeathenEngineering.SteamworksIntegration.API.Input.Client;
```

Now you can call its methods succinctly:

```csharp
SteamInput.GetControllers();
SteamInput.GetAction(...);
```

This approach preserves the uniqueness of each type while keeping your code concise and readable.

### Conclusion

Namespaces are not just about organizing your code—they’re essential for preventing naming conflicts and ensuring your development environment provides the best possible support. The `using` directive, particularly with aliasing, offers a powerful way to manage long or complex namespaces, making your code more maintainable and easier to understand.

Always define your code within a namespace. It’s a simple practice that pays off enormously as your projects grow and incorporate more external libraries or APIs.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://kb.heathen.group/game-dev-resources/game-programming/namespace-and-using.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
