Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I agree to ban the "using" directive from my C# projects? [closed]

Tags:

c#

.net

My colleague insists on explicitly specifying the namespace in code as opposed to using the using directive. In other words he wants to use the fully qualified name for each type every time this type occurs in code. Something like

public class MyClass
{
    public static void Main()
    {
        System.Console.WriteLine("Foo");
    }
}

instead of:

using System;
public class MyClass
{
    public static void Main()
    {
        Console.WriteLine("Foo");
    }
}

You can imagine the consequences.

The pros he gives:

  1. It's simpler to copy and paste code into other source files.
  2. It is more readable (you see the namespaces right away).

My cons:

  1. I have to write more
  2. The code is less readable (I guess de gustibus non disputandum est)
  3. No one does it!

What do you think about this?

like image 888
Marcin K Avatar asked Sep 11 '25 01:09

Marcin K


2 Answers

If you need to copy and paste code around so much as to actually benefit of having fully qualified types, you've got bigger problems.

Also, do you plan on remembering which namespace every single class is in in order to be able to type it fully qualified?

like image 184
Franci Penov Avatar answered Sep 12 '25 15:09

Franci Penov


For a slightly different answer: LINQ.

Extension methods are obtained only via "using" statements. So either the query syntax or the fluent interface will only work with the right "using" statements.

Even without LINQ, I'd say use "using"... reasoning that the more you can understand in fewer characters, the better. Some namespaces are very deep, but add no value to your code.

There are other extension methods too (not just LINQ) that would suffer the same; sure, you can use the static class, but the fluent interface is more expressive.

like image 41
Marc Gravell Avatar answered Sep 12 '25 14:09

Marc Gravell