# Using Keyword in C\#

#### **Using Keyword in C#**

The `using` keyword in C# is a powerful feature that simplifies resource management, namespace inclusion, and type aliasing. It has several key uses that are essential for efficient and clean code development in C#.

#### **Key Concepts**

1. **Namespace Inclusion**
   * The most common use of the `using` keyword is to include namespaces in your code. This allows you to access classes, methods, and other members defined in those namespaces without needing to fully qualify their names.
2. **Disposable Resource Management**
   * The `using` statement is also used to ensure that objects that implement the `IDisposable` interface are properly disposed of, freeing up resources and improving application performance and reliability.
3. **Type Aliasing**
   * The `using` directive can create an alias for a long or complex type name, making your code easier to read and maintain.

#### **Examples**

**Namespace Inclusion**

```csharp
using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
        Console.WriteLine("Numbers: " + string.Join(", ", numbers));
    }
}
```

**Disposable Resource Management**

```csharp
using System;
using System.IO;

class Program
{
    static void Main()
    {
        string path = "example.txt";
        using (StreamWriter writer = new StreamWriter(path))
        {
            writer.WriteLine("Hello, World!");
        } // StreamWriter is disposed of here, closing the file.
    }
}
```

**Type Aliasing**

```csharp
using System;
using Project = System.Collections.Generic.Dictionary<string, string>;

class Program
{
    static void Main()
    {
        Project projectDetails = new Project
        {
            { "Name", "C# Project" },
            { "Type", "Console Application" }
        };

        foreach (var detail in projectDetails)
        {
            Console.WriteLine($"{detail.Key}: {detail.Value}");
        }
    }
}
```

#### **Advanced Features**

1. **Static Using Statements**
   * Allows you to import static members of a class so you can use them without qualifying the class name.

```csharp
using static System.Math;

class Program
{
    static void Main()
    {
        double result = Sqrt(16); // No need to qualify with Math.Sqrt
        Console.WriteLine(result);
    }
}
```

2. **Global Using Statements (C# 10)**
   * Introduced in C# 10, these allow you to define `using` directives at the project level so they are available in all files within the project.

```csharp
// GlobalUsings.cs
global using System;
global using System.Collections.Generic;
```

#### **Summary**

* **Namespace Inclusion**: Simplifies the code by allowing access to types without fully qualifying their names.
* **Disposable Resource Management**: Ensures resources are properly released with the `using` statement.
* **Type Aliasing**: Improves readability by creating shorter names for complex types.
* **Static Using**: Provides a way to use static members without class qualification.
* **Global Using**: Makes `using` directives available across the entire project.


---

# 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://sannlynnhtun.gitbook.io/dotnet-developer/c-basic/using-keyword-in-c.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.
