Sacha K
Sacha K

Reputation: 642

Why is Default.aspx associated class called _Default?

I'm using a shared file to keep company name, copyright, version, ... in sync between all projects in a solution. This file also contains

[assembly: CLSCompliant(true)]

and I need to mark my DLLs as CLS compliant. In asp.net web forms applications the class associated with Default.aspx is called _Default and that is not CLS compliant and generates a warning.

Renaming the class to Default seems OK. Is there any downside? Why is the class called _Default?

Upvotes: 4

Views: 1112

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273784

You can only get away with Default because C# is case-sensitive. default is a reserved word.

And the genrator/template adds the extra _ because ASP.NET can also be used with VB.NET and Default (case insensitive) is also reserved in VB.

The standard template names it Default.ASPX because that is a 'standard document` in the default configuration of IIS.

Your class name does not really have to match the ASPX file (URL) name, you can call it class DefaultPage or something. Just fix the Inherits="..." attribute.

Upvotes: 5

Jon Hanna
Jon Hanna

Reputation: 113382

The naming of classes is algorithmic. Generally it will base the name on that of the file, and hence foo.aspx and Bar.aspx will result in foo and Bar classes.

Since default is a C# keyword (case-sensitive) and Default is a VB.NET keyword (case-insensitive), the automatic naming uses an underscore to avoid those keywords.

(It's worth noting that CLSCompliance isn't often very important in ASP.NET projects, where there won't be other code written in other .NET languages using the assembly, making this approach to avoiding keywords all the more reasonable.)

There's no harm in changing the name; the name was only given to you as a convenience to stop you having to type that code.

I'd recommend avoiding Default partly because you're going to collide with a keyword in VB.NET, but mostly because since you are not an algorithm, you should be able to think of a more meaningful name for what that class (and its associated page) does. That said, I'll admit to often using DefaultPage when I can't really think of much beyond "be the default page for that folder" at the time of coding.

Upvotes: 2

Related Questions