Default interfaces

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
 
G

Guest

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
 
G

Guest

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
 
M

Morten Wennevik

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();
 
M

Mark Rae

As Rakesh said, they using directives, not interfaces.

Yeah, not thinking...
They make it possible to use things like

Yeah, I know what they do, I was just wondering whether it was worth
removing the unused ones or not...
 
B

Bob Grommes

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top