Initial class name for Default page is _Default instead of Default

  • Thread starter Thread starter Sean Dockery
  • Start date Start date
S

Sean Dockery

When I create a new Web Application project, Visual Studio creates a web
application project with a Default page. The Default page is associated to a
class named _Default. If I delete the Default page and create a new page
named Default, the code behind class is named Default (instead of _Default).
Why does Visual Studio both naming the class _Default in the first place?
This is something that I have always been curious about.
 
When I create a new Web Application project, Visual Studio creates a web
application project with a Default page.  The Default page is associated to a
class named _Default.  If I delete the Default page and create a new page
named Default, the code behind class is named Default (instead of _Default).  
Why does Visual Studio both naming the class _Default in the first place? 
This is something that I have always been curious about.

The class names used in code files have to be unique across the whole
application.
If you create a subfolder and add a default page then the class name
will be in the form:

foldername_default

For the application root is doesn't have to be _default but that might
be an artifact of the site creation process which follows the above
regime with a null in place of the folder name. It would appear from
your observations that when a page is added manually it doesn't quite
follow the same logic and omits the underscore for the application
root (so long as the resultant name doesn't already exist in which
case it adds a digit e.g. default2).

It is probably just an accidental inconsistency in the VS logic.

HTH
 
This is because Default is a keyword, so Visual Studio uses _Default instead
(see the following in the documentation for an example of Default being a
keyword).

ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.en/dv_vbalr/html/45fce9b9-d212-4b2d-ab86-6e359b8b57af.htm
 
That makes sense, except that using Default as the class name seems to work
just fine, at least for C#.

Curiously, I just tried a few minutes ago to perform the same set of steps
of deleting and creating a web form page named Default. In C#, the class was
named Default, but in VB, the class was *still* named _Default. If I try to
change the class name manually in the VB project to be Default, Visual Studio
complains that a keyword cannot be used as an identifier.

I would guess that Microsoft names the class _Default in C# to be consistent
with the vanilla web application project that is generated for VB.

Thanks, Nathan, for pointing out the "other" language angle. I am sure that
I will find other strange things that are probably the way that they are to
satisfy all .NET languages.
 
Back
Top