33
[.NET 6][C#] using "using directive"
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".
- .NET 6.0.100
When I refer classes what are in other namespaces from a class,
I can omit writing full namespaces by using directive.
...
namespace ApprovementWorkflowSample.Applications
{
public interface IApplicationUserService
{
...
}
}
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;
}
...
}
}
If there are two or more classes (interfaces, etc.) in different namespaces, I can't omit the namespaces.
...
namespace ApprovementWorkflowSample.Users
{
public interface IApplicationUserService
{
...
}
}
...
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;
}
...
}
}
To avoid this error, I can write full namespaces or use aliases.
...
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;
}
If the alias is as same as another class(or interface, etc.) name, I will get compiling errors.
namespace ApprovementWorkflowSample.Controllers;
public class ApUsers
{
}
...
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;
}
To avoid this, I can use "::".
...
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;
}
I can use static methods or static members.
...
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");
}
I can use "global using" and omit write full namespaces.
// 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
{
...
}
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;
}
...
}
}
If I add a class what is named "AppUserService", I will get a compiling error.
namespace ApprovementWorkflowSample.Controllers;
public class AppUserService
{
}
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;
}
...
}
}
I can omit "using System;", "using Microsoft.Extensions.Logging;", etc. by "ImplicitUsings".
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
...
</ItemGroup>
</Project>
33