Any rules how to name C# source files?
-
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?
-
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?
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
-
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?
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.
-
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?
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 anEnum
. 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)
-
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?