您无法像在 C++ 中那样在 C# 中将“块”设为公共或私有,您必须为每个成员添加可见性(和实现)。在 C++ 中,你通常会这样做;
public:
memberA();
memberB();
private:
memberC();
...并在其他地方实现你的成员,而在 C# 中,你需要这样做;
public memberA() { ...implement your function here... }
public memberB() { ...implement your function here... }
private memberC() { ...implement your function here... }
至于属性,将它们视为自动实现的set and get您可以选择自己实现或让编译器实现它们的方法。如果您想自己实现它们,您仍然需要该字段来存储数据,如果您将其留给编译器,它也会生成该字段。
继承的工作方式与将内容放在同一个文件中的方式完全相同(对于较大的 C++ 项目来说,这可能不是一个好主意)。照常继承即可,只要在同一个命名空间或者导入了基类的命名空间,就可以无缝继承;
using System.Collections; // Where IEnumerable is defined
public class MyEnumerable : IEnumerable { // Just inherit like it
... // was in the same file.
}