DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Masui Masanori
Masui Masanori

Posted on

[.NET 6][C#] using "using directive"

Intro

From .NET 6(C# 10), I can use "global using" and "ImplicitUsing".
And I have never used "using static".

This time, I will try using "using directive".

Environments

  • .NET 6.0.100

Try

Basic usage

When I refer classes what are in other namespaces from a class,
I can omit writing full namespaces by using directive.

IApplicationUserService.cs

...
namespace ApprovementWorkflowSample.Applications
{
    public interface IApplicationUserService
    {
...
    }
}
Enter fullscreen mode Exit fullscreen mode

UserController.cs

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

// To refer "ApprovementWorkflowSample.Applications.IApplicationUserService.cs" 
using ApprovementWorkflowSample.Applications;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;
        private readonly IApplicationUserService users;
        public UserController(ILogger<UserController> logger,
                IApplicationUserService users)
        {
            this.logger = logger;
            this.users = users;
        }
...
    }
}
Enter fullscreen mode Exit fullscreen mode

alias

If there are two or more classes (interfaces, etc.) in different namespaces, I can't omit the namespaces.

IApplicationUserService.cs

...
namespace ApprovementWorkflowSample.Users
{
    public interface IApplicationUserService
    {
...
    }
}
Enter fullscreen mode Exit fullscreen mode

UserController.cs

...
using ApprovementWorkflowSample.Applications;
using ApprovementWorkflowSample.Users;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;

        // Compiling error
        private readonly IApplicationUserService users;
        public UserController(ILogger<UserController> logger,
                // Compiling error
                IApplicationUserService users)
        {
            this.logger = logger;
            this.users = users;
        }
...
    }
}
Enter fullscreen mode Exit fullscreen mode

To avoid this error, I can write full namespaces or use aliases.

UserController.cs

...
using ApUsers = ApprovementWorkflowSample.Applications;
using ApprovementWorkflowSample.Users;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;
        // OK
        private readonly ApprovementWorkflowSample.Applications.IApplicationUserService users;
        public UserController(ILogger<UserController> logger,
                // OK
                ApUsers.IApplicationUserService users)
        {
            this.logger = logger;
            this.users = users;
        }
Enter fullscreen mode Exit fullscreen mode

If the alias is as same as another class(or interface, etc.) name, I will get compiling errors.

ApUsers.cs

namespace ApprovementWorkflowSample.Controllers;
public class ApUsers
{   
}
Enter fullscreen mode Exit fullscreen mode

UserController.cs

...
using ApUsers = ApprovementWorkflowSample.Applications;
using ApprovementWorkflowSample.Users;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;
        private readonly ApprovementWorkflowSample.Applications.IApplicationUserService users;
        public UserController(ILogger<UserController> logger,
                // Compiling error
                ApUsers.IApplicationUserService users)
        {
            this.logger = logger;
            this.users = users;
        }
Enter fullscreen mode Exit fullscreen mode

To avoid this, I can use "::".

UserController.cs

...
using ApUsers = ApprovementWorkflowSample.Applications;
using ApprovementWorkflowSample.Users;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;
        private readonly ApprovementWorkflowSample.Applications.IApplicationUserService users;
        public UserController(ILogger<UserController> logger,
                // OK
                ApUsers::IApplicationUserService users)
        {
            this.logger = logger;
            this.users = users;
        }
Enter fullscreen mode Exit fullscreen mode

using static

I can use static methods or static members.

UserController.cs

...
using static System.Console;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;
        private readonly ApprovementWorkflowSample.Applications.IApplicationUserService users;
        public UserController(ILogger<UserController> logger,
                ApUsers::IApplicationUserService users)
        {
...
            WriteLine("Hello");
        }
Enter fullscreen mode Exit fullscreen mode

global using

I can use "global using" and omit write full namespaces.

IApplicationUserService.cs

// I must write "global using" on the top of the file or I will get a compiling error.
global using AppUserService = ApprovementWorkflowSample.Applications.IApplicationUserService;
...
namespace ApprovementWorkflowSample.Users;
public interface IApplicationUserService
{
...
}
Enter fullscreen mode Exit fullscreen mode

UserController.cs

using ApUsers = ApprovementWorkflowSample.Applications;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;

        // I don't need writing using directive.
        private readonly AppUserService users;

        public UserController(ILogger<UserController> logger,
                // Of cource, the type of "AppUserService" is as same as "IApplicationUserService".
                ApUsers::IApplicationUserService users)
        {
            this.logger = logger;
            this.users = users;
        }
...
    }
}
Enter fullscreen mode Exit fullscreen mode

using same name?

If I add a class what is named "AppUserService", I will get a compiling error.

AppUserService.cs

namespace ApprovementWorkflowSample.Controllers;
public class AppUserService
{   
}
Enter fullscreen mode Exit fullscreen mode

UserController.cs

using ApUsers = ApprovementWorkflowSample.Applications;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace ApprovementWorkflowSample.Controllers
{
    public class UserController: Controller
    {
        private readonly ILogger<UserController> logger;
        // this treated as "ApprovementWorkflowSample.Controllers.AppUserService"
        private readonly AppUserService users;

        public UserController(ILogger<UserController> logger,
                ApUsers::IApplicationUserService users)
        {
            this.logger = logger;
            // Compiling error.
            this.users = users;
        }
...
    }
}
Enter fullscreen mode Exit fullscreen mode

ImplicitUsings

I can omit "using System;", "using Microsoft.Extensions.Logging;", etc. by "ImplicitUsings".

ApprovementWorkflowSample.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>
  <ItemGroup>
...
  </ItemGroup>
</Project>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Create an Account!
Now it's your turn!
Β 
πŸ—’ Share a tutorial
πŸ€” Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.