C# 单个 .cs 文件中的多个类 - 好还是坏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11771047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:10:32  来源:igfitidea点击:

Multiple classes in a single .cs file - good or bad?

c#c#-4.0

提问by Learner

Possible Duplicate:
Is it a bad practice to have multiple classes in the same file?

可能的重复:
在同一个文件中有多个类是一种不好的做法吗?

Is it advisable to create multiple classes within a .cs file or should each .cs file have an individual class?(Class file name also Animal.cs)

是否建议在 .cs 文件中创建多个类,还是每个 .cs 文件都应该有一个单独的类?(类文件名也是 Animal.cs)

public class Animal
{
}

public class Person
{
}

public class Utility
{
}

采纳答案by TechGuy

Simply say good practice is one class from one file. BUT if your application is quite small you can use several classes in one file.

简单地说,好的做法是一个文件中的一个类。但是,如果您的应用程序非常小,您可以在一个文件中使用多个类。

回答by Paolo del Mundo

For the sake of making things simple, you should probably put them in different files. I can't think of any advantages of keeping them in the same file.

为了简单起见,您可能应该将它们放在不同的文件中。我想不出将它们保存在同一个文件中的任何好处。

回答by Vinoth

When designing classes we respect the Single Responsibility Principle. Reading code becomes a lot easier if its shape follows its semantics, hence splitting files by class is sensible.

在设计类时,我们尊重单一职责原则。如果代码的形状遵循其语义,则阅读代码会变得容易得多,因此按类拆分文件是明智的。

However if there are inner classes it makes sense to keep them in the same file

但是,如果有内部类,将它们保存在同一个文件中是有意义的

Source: Old Post

来源:旧帖

回答by Ionic? Biz?u

It depends about your application. If it is a big application, I think that isn't good. Data must be very organized.

这取决于您的应用程序。如果是一个大的应用程序,我认为这并不好。数据必须非常有条理。

I think that is better to keep them in different files.

我认为最好将它们保存在不同的文件中。

But, if you are working for a small application, it is good to keep them in same file.

但是,如果您正在为一个小型应用程序工作,最好将它们保存在同一个文件中。

Hope that I helped you.

希望我对你有所帮助。

回答by Niks

Logically its depend on your scope of application. But Normally best practice to do coding in seperate class (New File , new class )

从逻辑上讲,它取决于您的应用范围。但通常最好的做法是在单独的类中进行编码(新文件,新类)

Splittting files will be more sensible.Because industries follow ths standards...

拆分文件会更明智。因为行业遵循这些标准......