Default interfaces

  • Thread starter Thread starter Mark Rae
  • Start date Start date
M

Mark Rae

Hi,

Looking for opinions again... :-)

When creating a new WinForm or WebForm in C#, the .cs code-behind file is
created automatically with some default interfaces at the top, e.g. in the
case of a WebForm,

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

That's fine, of course, but I'm interested to know if there is any benefit
in removing those interfaces which will not be required by any subsequent
code on the page. E.g. if the form doesn't need to interface with a
database, there's no need for System.Data to be referenced. Similarly, if
you're not working with ArrayLists or HashTables, you can safely remove
System.Collections.

Is this worth doing? Does each reference to an interface consume server
resources even if it isn't used? Do developers delete the unused references
or just leave them? Does it matter?

Mark
 
Hi Mark,

They are not interfaces, but namespaces.

These using directives will only be creating aliases to the namespaces and
nothing else. It won't consume resources. They are only meant to help you not
type the whole name. No harm in keeping them. :)

HTH,
Rakesh
 
Hi Mark,

They are not interfaces, but namespaces.

These 'using' directives will only be creating aliases to the namespaces and
nothing else. It won't consume resources. They are only meant to help you not
type the whole name. No harm in keeping them. :)

HTH,
Rakesh
 
Hi Mark,

As Rakesh said, they using directives, not interfaces. They make it possible to use things like

MessageBox.Show();

instead of

System.Windows.Forms.MessageBox.Show();

They are used by the compiler to locate class definitions, and leaving them won't do any harm.

However, they do add a risk of duplicate definitions.

using System.Windows.Forms;
using System.Web.UI.WebControls;

Button b = new Button(); // compiler error

Button is defined in both namespaces so the compiler won't know which one you want to use.
Same goes for things like Timer which is defined in three different namespaces.
In these cases, you can either remove the unneeded namespaces, add the full name like System.Windows.Forms.Button, or create your own aliases.

using MyButton = System.Windows.Forms.Button;

MyButton b = new MyButton();
 
There is no runtime overhead. Just a slight (probably insignificant)
compile-time overhead since the compiler will have more using directives to
search through to resolve names. Having too many of them also raises the
possiblity of naming conflicts where the same class exists in two
namespaces. In practice though, that's happened to me like once in two
years, and it's fairly easy to recognize and deal with.

The default directives are safe for typical projects, and are probably
needed sooner or later in most of them.

--Bob
 
Back
Top