Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Any rules how to name C# source files?

Any rules how to name C# source files?

Scheduled Pinned Locked Moved C#
tutorialcsharpvisual-studiocomhelp
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CRobert456
    wrote on last edited by
    #1

    What are the specific rules to name a C# source file? I came across many posts about this issue but all are about special cases. For example, Microsoft's page about this topic does not really get to the point. https://msdn.microsoft.com/en-us/library/ms228500%28v=vs.90%29.aspx[^] I need clear and to-the-point answers. Is a C# source file name arbitrary? I am using code samples from several C# books and the samples tend to pick up the name of the top (non-nested) class in the file or the name of namespace, or maybe something else. I have not tried to play with this yet but my feeling is that the file name could be just arbitrary. Is it correct? What are the imposed restrictions on file name and what are the actual guidelines?

    S P L D 4 Replies Last reply
    0
    • C CRobert456

      What are the specific rules to name a C# source file? I came across many posts about this issue but all are about special cases. For example, Microsoft's page about this topic does not really get to the point. https://msdn.microsoft.com/en-us/library/ms228500%28v=vs.90%29.aspx[^] I need clear and to-the-point answers. Is a C# source file name arbitrary? I am using code samples from several C# books and the samples tend to pick up the name of the top (non-nested) class in the file or the name of namespace, or maybe something else. I have not tried to play with this yet but my feeling is that the file name could be just arbitrary. Is it correct? What are the imposed restrictions on file name and what are the actual guidelines?

      S Offline
      S Offline
      Sascha Lefevre
      wrote on last edited by
      #2

      Well, it sort of gets to the point:

      Quote:

      Unlike Java, source files can contain more than one top-level public class declaration, and the file name does not need to match any of the classes' names.

      So you wouldn't violate an official naming rule by naming your files what you deem most appropriate. Normally they should contain only one class and have the same name as that class because that's what's most intuitive. In my opinion, it sometimes makes sense to have more than one class in a source file. In that case I would name the file like the class that is either the base class or represents the functionality that is most important within that source file. In some cases, e.g. lots of small classes that serve a very similar purpose, I put them all in a single source file and name it according to the purpose of those classes. But it should remain the exception from the rule.

      If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

      1 Reply Last reply
      0
      • C CRobert456

        What are the specific rules to name a C# source file? I came across many posts about this issue but all are about special cases. For example, Microsoft's page about this topic does not really get to the point. https://msdn.microsoft.com/en-us/library/ms228500%28v=vs.90%29.aspx[^] I need clear and to-the-point answers. Is a C# source file name arbitrary? I am using code samples from several C# books and the samples tend to pick up the name of the top (non-nested) class in the file or the name of namespace, or maybe something else. I have not tried to play with this yet but my feeling is that the file name could be just arbitrary. Is it correct? What are the imposed restrictions on file name and what are the actual guidelines?

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        There are no real rules around the way you name class files, but something to consider is what the purpose of your naming is. Generally, it's a good idea to name the file the same as the class because it makes finding your class a lot easier. If your class is called Foo but you name it Bar.cs then it's going to be harder to find the file in the solution. Generally, it makes sense to try to replicate your namespace structure with folders and your classes in the file name.

        1 Reply Last reply
        0
        • C CRobert456

          What are the specific rules to name a C# source file? I came across many posts about this issue but all are about special cases. For example, Microsoft's page about this topic does not really get to the point. https://msdn.microsoft.com/en-us/library/ms228500%28v=vs.90%29.aspx[^] I need clear and to-the-point answers. Is a C# source file name arbitrary? I am using code samples from several C# books and the samples tend to pick up the name of the top (non-nested) class in the file or the name of namespace, or maybe something else. I have not tried to play with this yet but my feeling is that the file name could be just arbitrary. Is it correct? What are the imposed restrictions on file name and what are the actual guidelines?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          CRobert456 wrote:

          I have not tried to play with this yet but my feeling is that the file name could be just arbitrary. Is it correct?

          Yup, if you generate a GUID and use that as a name, things will work too. Wouldn't be very practical. It is often yelled that each file should contain a single class, and be named after it. That does lead to a "lot" of files if you stoically do so. There's often some helper-classes that are local to the form, like an inherited EventArgs, or an Enum. If they're really local, they become nested classes within the same file. Keeps things in a place that is a bit predictable :)

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)

          1 Reply Last reply
          0
          • C CRobert456

            What are the specific rules to name a C# source file? I came across many posts about this issue but all are about special cases. For example, Microsoft's page about this topic does not really get to the point. https://msdn.microsoft.com/en-us/library/ms228500%28v=vs.90%29.aspx[^] I need clear and to-the-point answers. Is a C# source file name arbitrary? I am using code samples from several C# books and the samples tend to pick up the name of the top (non-nested) class in the file or the name of namespace, or maybe something else. I have not tried to play with this yet but my feeling is that the file name could be just arbitrary. Is it correct? What are the imposed restrictions on file name and what are the actual guidelines?

            D Offline
            D Offline
            Dennis_E
            wrote on last edited by
            #5

            Consider generic types with the same name, like Tuple. You could put

            Tuple, Tuple, Tuple

            , etc in separate files (Tuple.cs, Tuple`1.cs, Tuple`2.cs), but it's acceptible to put them in one file.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups