在C#中使用“私有”类的类修饰符问题(class modifier issues in C# with “private” classes)

编程入门 行业动态 更新时间:2024-10-11 01:10:12
在C#中使用“私有”类的类修饰符问题(class modifier issues in C# with “private” classes)

我有一个有很多方法的课程:

public class MyClass { public bool checkConditions() { return checkCondition1() && checkCondition2() && checkCondition3(); } ...conditions methods public void DoProcess() { FirstPartOfProcess(); SecondPartOfProcess(); ThirdPartOfProcess(); } ...process methods }

将这两个类都设置为MyClass的内部类?

我有2个选项:我在MyClass定义它们,将所有内容都放在同一个文件中,这看起来令人困惑和丑陋,或者我可以将MyClass定义为一个partial class , MyClass有一个文件, MyClassConditions有另一个文件, MyClassConditions其他。

将它们定义为内部?

我不太喜欢那些内部修饰符,因为我没有发现这些类为我的程序/程序集的其余部分添加了任何值,并且我想尽可能隐藏它们。 这并不像他们在程序的其他任何部分会有用/可重用。

让他们公开?

我看不出为什么,但我已经让这个选项在这里。

任何其他?

命名它!

谢谢

I had a class that had lots of methods:

public class MyClass { public bool checkConditions() { return checkCondition1() && checkCondition2() && checkCondition3(); } ...conditions methods public void DoProcess() { FirstPartOfProcess(); SecondPartOfProcess(); ThirdPartOfProcess(); } ...process methods }

Setting both classes as inner classes of MyClass?

I have 2 options: I either define them inside MyClass, having everything in the same file, which looks confusing and ugly, or I can define MyClass as a partial class, having one file for MyClass, other for MyClassConditions and other for MyClassProcessExecution.

Defining them as internal?

I don't really like that much of the internal modifier, as I don't find these classes add any value at all for the rest of my program/assembly, and I'd like to hide them if possible. It's not like they're gonna be useful/reusable in any other part of the program.

Keep them as public?

I can't see why, but I've let this option here.

Any other?

Name it!

Thanks

最满意答案

你最好的选择可能是使用部分类,并将三个代码块放在单独的文件中,并添加到同一个类中。 然后,您可以将条件代码和流程代码设为私有,以便只有类本身才能访问它们。

Your best bet is probably to use partial classes and put the three clumps of code in separate files adding to the same class. You can then make the conditional and process code private so that only the class itself can access them.

更多推荐

本文发布于:2023-07-27 02:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284047.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:修饰符   class   modifier   classes   private

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!