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();
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();
Probably to help avoid type ambiguities.
Update: More elegantly described by Heinzi.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With