3

Possible Duplicate:
How to name C# source files for generic classes

We are currently re-evaluating how we do generic classes when we inherit from a general class. Currently we will put the following two class definitions in the same file

class Foo
{
    // code for class
}

class Foo<T> : foo
{
   // code for class
}

My question is a simple one, should we keep them in the same file, or split them into separate files?

So far the pros to keeping them in the same file is that you have all the code there right infront of you. The con is that when both classes get sufficiently large, it could become un-readable.

What I would like is good reasons as to why we should do one or the other. If you recommend separate file, I would also like you to include possible naming conventions, or a strategy to get around the fact that we can have only one file named Foo

Community
  • 1
  • 1
Zoidberg
  • 10,137
  • 2
  • 31
  • 53
  • A couple of the answers here link to [this question](http://stackoverflow.com/questions/3108189/how-to-name-c-sharp-source-files-for-generic-classes/3108243#3108243), which seems to be asking the same thing. – Matthew Strawbridge Dec 30 '11 at 17:14

5 Answers5

5

This is a matter of opinion, but I'd keep them in the same file rather than try to maintain some naming convention for one or the other.

While I subscribe to one class, one file, I think there is value in having these together. We really treat these as one class, right? Typically, Foo will be abstract, and is just a way of using our generic types… well, more generically -- in places where the type parameters don't matter and can't be known at compile time.

If the classes become too large, it should be a red flag anyway that some responsibilities should be broken out.

Jay
  • 56,361
  • 10
  • 99
  • 123
1

Keep these classes small and you can keep them in one file. If you can't keep them small, divide them. If you prefer keeping them in separate files, it's okay too. But keep them small anyway. In case of separate file, I would use FooGeneric name but someone here How to name C# source files for generic classes recommends Foo`1 (for 1 parameter).

Community
  • 1
  • 1
Pol
  • 5,064
  • 4
  • 32
  • 51
1

Unless classes are utterly trivial, I never put more than one in a single file. It's much easier, IMO, to find exactly the class you seek when you have a predictable, unique file name, with namespaces based on folders, generally.

For naming your files, maybe this:

foo.cs
foo_t.cs
foo_tuv.cs // for a foo class with three generics
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
1

I'd recommend keeping the classes in the same file. It makes it easier to locate all Foo classes. Also, with code folding (regions) you can easily view only a single class by collapsing the other.

That said, I wouldn't say either way is wrong. In the end this is one of those things that will take some experience to come up with your personal preference and find what works for you in your particular project. And you may find that what works well for one project doesn't necessarily work for your next project.

Michael Minton
  • 4,447
  • 2
  • 19
  • 31
1

Answered here:

I think the common solution to this problem is to name the file like this:

{ClassName}`{NumberOfGenericParameters}

This would give you this filename:

Bag.cs and Bag`1.cs

This is the way Microsoft handle this issue in frameworks like Asp.net Mvc.

Community
  • 1
  • 1
Jesse Smith
  • 963
  • 1
  • 8
  • 21