Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No "using" reference in asp.net generated code

When I looked at the generated code of an aspx page, one thing struck me. Every class reference is printed every time instead of applying the "using" method at the top of the code file.
Is there a reason for doing this?
If there's no difference in the two methods then why not use "using" for simplicity?

System.Data.DataSet theSet = new DataSet();

vs

using System.Data;  
DataSet theSet = new DataSet();
like image 527
Niklas Avatar asked Jul 15 '26 10:07

Niklas


2 Answers

Because for generated code, simplicity of reading is not a priority.

Simplicity of writing is an issue, however: If types are always specified with their fully qualified name, the likelihood of a name conflict decreases. Imagine that you have two libraries that provide TextBox controls, and you add both of them to your web form.

// no problem
System.Web.UI.WebControls.TextBox myDefaultTextBox = new System.Web.UI.WebControls.TextBox();
CustomLibrary.TextBox theOtherTextBox = new CustomLibrary.TextBox();

as compared to

using System.Web.UI.WebControls;  
using CustomLibrary;  

// won't compile, would need special treatment by the code generator
TextBox myDefaultTextBox = new TextBox();
TextBox theOtherTextBox = new TextBox();
like image 177
Heinzi Avatar answered Jul 18 '26 00:07

Heinzi


Probably to help avoid type ambiguities.

Update: More elegantly described by Heinzi.

like image 34
Nathan Avatar answered Jul 18 '26 00:07

Nathan