简体   繁体   中英

If I use a baseController class do I need to specify USING in the children?

If my base class has something like:

using System.Web.Mvc;
using System.Web.Routing;
using MFA.Storage.Helpers;
using MFA.Storage.Models;
using MFA.WebUx.Areas.Administration.ViewModels.Notes;
using MFA.WebUx.Areas.Administration.Services.Notes;
using MFA.WebUx.Areas.Administration.ActionFilters
using System;

Then in my child classes that inherit from the base controller do I still need to add all the using directives? Is that a good or bad practice?

using directives do not "inherit" (their scope is confined to the file in which they are declared), so you will need to add them to the descendant class as well, unless both classes reside in the same file.

From a best practice perspective, it's probably better to keep each class in it's own file, and add using directives to each class file as needed.

You definitely don't NEED those using directives if you don't use them. The only one you need for sure is the namespace of the base controller.

However, it's not really considered "bad practice" to include them ... many of Visual Studio's default files have several using s that aren't used by default. As long as the extra using s don't create conflicts, then just keep them there. It's much easier to leave them there than it is to add them back later.

As a side note, there are some GREAT plugins that help you organize the using directives. I use Resharper, which "grays-out" any useless using directives, indicating that they can be removed. And when a using directive is missing, it finds it and lets me add it easily.
Visual Studio 2010 also lets you add missing using directives, so it's not a big deal to remove extra using s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM