Fog Creek Software
Discussion Board

Regarding multiple classes in a single C# file

A while ago, there was a thread about putting defining multiple classes in a single .CS file. While the general consensus was that it wasn't such a great idea, I stumbled across another reason to be careful about doing it.

I saw a blog from a member of the C# team discussing how resource files are compiled under .NET and he mentions how VS makes an assumption that you have only a single class defined in a source file.

"Rather than the designer just spitting out the name it used, and then resgen reading it out of the .RESX file (or any number of other sensible options), the project system has to sniff your sources and guess what name the designer used.  It does this by using the “Default Namespace” project property setting (which doesn't always match the actual namespace used int eh source file), and then assumes that everybody only puts on class in each source file, and thus uses the first class in the source file.  It combines that information to produce the proper .RESOURCES filename which is then used as the string identifier in metadata because they don't bother to override the default."

Here's the entire post:

Mark Hoffman
Saturday, April 10, 2004

There is a similar thing to this in the VS IDE, I can't remember the exact way it didn't work but I think it went like if you put another class in one of your forms CS files before/above the form class the VS designer would say there was something wrong with your CS file because it always assumed that there was only one class in each file (or that the form would be first if there was more than one)

So I would say that the problem you described is similar to this one (if it still occurs that is, I think it may have been during the beta)


Chris Ormerod
Saturday, April 10, 2004

*  Recent Topics

*  Fog Creek Home