开发者

Is there any objective reason not to remove unused default includes from C# program?

开发者 https://www.devze.com 2023-01-20 03:30 出处:网络
Is there any objec开发者_Go百科tive reason not to remove unused default includes from C# program?

Is there any objec开发者_Go百科tive reason not to remove unused default includes from C# program?

Let's take this hello world project as an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("Hello world");
        }
    }
}

Program works fine with the

using System.Collections.Generic;
using System.Linq;
using System.Text;

part commented out.

While this example is very simple, I don't see at this point any good reason to keep those three using lines in program.

"I might need that later" comes to mind as a reason, but that would be true for any library which comes with compiler.

I just want to say that I'm a beginner in C#, so I may be missing something really obvious.


No reason; I typically remove any "using" statements I'm not actually using. (And since I use ReSharper w/ Visual Studio, I can use their shortcuts for removing unused ones, and for adding them back if I use something form one of them in the future.)

Cleaner is better, in my opinion.


1) They are usings and not includes, so they don't suffer a performance penalty like in C/C++

2) Intellisense won't list the classes in there

3) Extensionsmethods don't work. Particularly annoying in case of System.Linq since LINQ consists of extension methods.


I regularly run the "remove unused usings and sort" over my entire codebase - one of my easy red-flags when reviewing a class file is too many using statements. It's normally an indication that the class I'm looking at is worrying itself with too many concerns.


It makes it a little easier to use common features (LINQ, collections) later.


The less using constructs you have, the better, it reduces ambiguity in the program and makes it much easier to read when the reader understands the exact type that you return and take as arguments. In my opinion, there is no valid reason to keep them.


You can remove these using statements if you don't like but there is no harm to keep as it is.

It totally depends n you , how you wanna see your code base?


I usually leave the using statements there until I am somewhat "done" with writing the class (I know a class isn't perfect first time).

Then there are options in Visual Studio context menu to a) remove unused Using statements, and b) sort Using statements. This applies to Visual Studio with no add-ons.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号