C# 类函数成员声明&执行

发布于 2024-08-06 20:43:52 字数 242 浏览 5 评论 0原文

C# 中是否存在类似于 C++ 中的类定义和实现的概念?

我更喜欢通过删除大多数(如果不是全部)实现细节来保持类定义简单(这取决于您可能知道的几个因素,但通常我倾向于将大多数成员实现细节保留在类定义之外)。这样做的好处是让我可以鸟瞰该类及其功能。

然而在 C# 中,我似乎被迫在声明时定义我的成员函数。这可以避免,或者以某种方式规避吗?

在我学习 C# 的过程中,这是困扰我的一个方面。类,尤其是复杂的类,变得越来越难读。

Is there a concept in C# of class definition and implementation similar to what you find in C++?

I prefer to keep my class definitions simple by removing most, if no every, implementations details (it depends on several factors as you may know, but generally I move towards leaving most member implementation details outside the class definition). This has the benefit of giving me a bird's eye view of the class and its functionality.

However in C# it seems I'm forced to define my member functions at the point of declaration. Can this be avoided, or circumvent some way?

During my apprenticeship of C#, this is one aspect that is bothering me. Classes, especially complex ones, become increasingly harder to read.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

酒儿 2024-08-13 20:43:53

如果您发现某个类难以阅读或难以理解,这通常表明该类试图做太多事情。不要尝试复制 C++ 的声明和定义分离,而是考虑将麻烦的类重构为多个类,以便每个类承担更少的责任。

If you find that a class is hard to read or difficult to understand, that's often a sign that the class is trying to do too much. Instead of trying to duplicate C++'s separation of declarations and definitions, consider refactoring the troublesome class into several classes so that each class has less responsibility.

神仙妹妹 2024-08-13 20:43:53

只要有可能或需要,我就会采用之前的响应并定义一个接口。但这并不总是合适的。

或者,您可以使用一些静态代码检查工具来解决这个“问题”。 Resharper 的“文件结构”窗口将为您提供您想要的内容。您还可以使用 Visual Studio 中内置的“类视图”。但我更喜欢前者。

Whenever it's possible or desirable, I'll go with the previous responses and define an interface. but it's not always appropriate.

alternatively, you can work around this "problem" by using some static code inspection tools. Resharper's "File Structure" window will give you exactly what you want. you can also use the built in "Class View" from visual studio. but I prefer the former.

情泪▽动烟 2024-08-13 20:43:53

我猜你所指的原型在 C# 中并不存在。按照其他人的建议定义接口将为您提供收集方法的声明,但这与原型不同,而且我不太确定它是否会帮助您使实现类更易于阅读。

C# 不是 C++,并且可能不应该被视为 C++。

The prototyping that I guess you are referring to does not really exist in C#. Defining interfaces as others have suggested will give you a point where you have declarations of your methods collected, but it's not the same thing as prototypes, and I am not so sure that it will help you in making your implementation classes easier to read.

C# is not C++, and should probably not be treated as C++.

云淡风轻 2024-08-13 20:43:53

不确定你的课程继续增长并变得难以阅读是什么意思。您的意思是您想要一个像类成员视图之类的头文件吗?如果是这样,就像约翰建议的那样,你不能折叠实现,这样你就不必看到它了吗?

如果您不希望每个类都实现某件事,那么接口可能是可行的方法(就像其他人所说的那样)。

但另一方面,如果你的类本身随着你编写程序而变得越来越复杂,也许这更多的是一个设计问题而不是一个语言问题?我认为一个类应该有一种责任,而不是随着程序的增长而承担越来越多的责任,而是随着您继续开发软件,类的数量和使用的类的年龄应该增长并变得更加复杂?

Not sure what you mean by your classes continue to grow and become hard to read. Do you mean you want a header file like view of a class's members? If so, like John suggested, can't you just collapse the implementation so you don't have to see it?

If you don't want every class to implement a certain thing, then interfaces are probably the way to go (like others are saying).

But as a side thought, if your classes themselves get more and more complex as a your write the program, perhaps it's more of a design issue than a language problem? I think a class should have one responsibility and not take on more and more responsibilities as the program grows, rather the number of classes and how old classes are used should grow and get more complex as you continue to develop your software?

信愁 2024-08-13 20:43:53

有两种补救措施可以使其更像 C++:

  • 创建一个声明所有方法签名和属性的接口文件
  • 通过在类定义上使用 partial 修饰符在多个文件中的类中实现该接口

编辑:

// File: ICppLikeInterface.cs
public interface ICppLikeInterface
{
    ...
}

// File: CppLikeImplementation1.cs    
public partial class CppLikeImplementation : ICppLikeInterface
{
    ...
}

// File: CppLikeImplementation2.cs    
public partial class CppLikeImplementation : ICppLikeInterface
{
    ...
}

C++ 将接口分离到头文件中的方式主要(我认为)是由于创建 C 时的早期设计决策,以允许在“旧时代”进行快速、增量编译,因为编译器会丢弃任何元数据,与 Smalltalk 相反。这对于 C#(也不是 Java)来说不是问题,在最新的硬件上,数万行代码可以在几秒钟内编译完成(C++ 仍然不能)

There are two remedies for this to make it more C++-ish:

  • Create an interface file that declares all method signatures and properties
  • Implement that interface in a class across multiple files by using the partial modifier on the class definitions

Edits:

// File: ICppLikeInterface.cs
public interface ICppLikeInterface
{
    ...
}

// File: CppLikeImplementation1.cs    
public partial class CppLikeImplementation : ICppLikeInterface
{
    ...
}

// File: CppLikeImplementation2.cs    
public partial class CppLikeImplementation : ICppLikeInterface
{
    ...
}

The C++ way of separating interface into a header file is mostly (I think) due to an early design decision when C was created to allow fast, incremental compilations during the "old days", as the compiler throws away any meta data, contrary to Smalltalk. This is not a matter with C# (nor Java) where tens of thousands of lines compiles within seconds on recent hardware (C++ still doesn't)

臻嫒无言 2024-08-13 20:43:52

这确实是一个需要退一步看大局的情况。 Visual Studio 有很多很多工具可以帮助您编写和操作代码,包括大纲、#regions、类视图、类图、代码定义窗口等等。

C# 不是 C++,如果你试图这样做,那么你就会被自己绊倒,而且没有人能够阅读你的代码。

花一天时间学习使用 Visual Studio 工具将在生产力方面获得数倍的投资回报,并且您很快就会想知道您是如何使用 C++ 的做事方式的。

根据评论进行更新

我早已不再将我的代码视为简单的文本文件。我将代码视为有机的东西,我发现允许自己依赖功能丰富的 IDE 可以让我更轻松地上下移动抽象级别,并无限提高我的工作效率。我想这可能是一种个人特质,也许并不适合所有人;我有一个非常“视觉”的头脑,当我能看到图片中的东西时,我的工作效果最好。

也就是说,聪明的 IDE 并不是糟糕风格的借口。有一些最佳实践可以编写不需要智能 IDE 的“干净代码”。干净代码的原则之一是保持事物的定义接近其使用,我认为这可以扩展到涵盖声明和定义。就我个人而言,我认为将声明和定义分开会使代码不太清晰。如果您发现怪物类别难以理解,那么这可能表明您违反了单一职责原则。

c/C++ 中单独定义和声明的原因是因为 C++ 使用单遍编译器,与 C# 及其 两遍编译器 无论声明的顺序如何,它都可以愉快地找到引用。这种差异源于编译器不同的设计理念:C/C++ 将每个源文件视为一个编译单元,而在 C# 中则将整个项目视为编译单元。我想当您习惯于以 C/C++ 方式工作时,分离声明和定义似乎是一种理想的风格元素,但我个人认为保留声明和使用(或者在本例中是声明和定义)会增强,而不是降低可读性。我自己曾经是一名 C 程序员,直到 2001 年开始使用 C#。我一直很喜欢 C,并认为它的做事方式是“蜜蜂膝盖”。这些天,当我阅读 C/C++ 代码时,我认为它看起来绝对可怕,我不敢相信我们曾经忍受这样的工作方式。我想这完全取决于你的习惯。

This is really a case of needing to step back and see the bigger picture. Visual studio has many, many tools to help you write and manipulate your code, from outlining, #regions, class view, class diagrams, the Code Definition Window and many more.

C# isn't C++, if you try to make it so then you'll trip over yourself and no-one else will be able to read your code.

A day spent learning to use the Visual Studio tools will repay the investment many times over in terms of productivity and you'll soon wonder how you ever lived with that C++ way of doing things.

Update in response to comments

I have long since stopped regarding my code as simple text files. I regard code as an organic thing and I find that allowing myself to rely on a feature-rich IDE lets me move up and down levels of abstraction more easily and enhances my productivity no end. I suppose that could be a personal trait and perhaps it is not for everyone; I have a very 'visual' mind and I work best when I can see things in pictures.

That said, a clever IDE is not an excuse for poor style. There are best practices for writing "clean code" that don't require an smart IDE. One of the principles of clean code is to keep the definition of something near its use and I think that could be extended to cover declaration and definition. Personally, I think that separating the declaration and definition makes the code less clear. If you are finding that you get monster classes that are hard to understand, then that might be a sign that you're violating the Single Responsibility Principle.

The reason for separate definition and declaration in c/C++ is because C++ uses a single pass compiler, where forward references cannot be resolved later, unlike C# and its two-pass compiler which can happily find references regardless of the order of declaration. This difference stems from the different design philosphies of the compilers: C/C++ considers each source file to be a unit of compilation, whereas in C# the entire project is considered to be the unit of compilation. I suppose when you are used to working in the C/C++ way then separating the declaration and definition can appear to be a desirable element of style, but I personally believe that keeping declaration and use (or in this case declaration and definition) enhances, rather then reduces, readability. I used to be a C programmer myself until I started using C# in 2001. I always loved C and thought it's way of doing things was the 'bees knees'. These days when I read C/C++ code I think it looks absolutely horrendous and I can't believe we used to put up with working that way. It's all a matter of what you are used to, I suppose.

一杯敬自由 2024-08-13 20:43:52

如果您使用 Visual Studio,则可以利用类视图。您还可以使用源代码编辑器的展开/折叠功能。

在不太可能的情况下,您的工具无法提供帮助,您始终可以编写一个快速实用程序来为您总结该类。

如果该类已编译,您也可以使用 Reflector 查看该类。

If you're using Visual Studio, you can take advantage of the Class View. You can also use the expand/collapse features of the source code editor.

In the improbable case that your tools don't help, you can always write a quick utility that will summarize the class for you.

If the class has been compiled, you can use Reflector to view the class, too.

谁把谁当真 2024-08-13 20:43:52

不,C# 中没有像 C/C++ 中那样的实现和头文件的概念。最接近的方法是使用接口,但接口只能定义类的公共成员。然后,您最终会得到类和接口的一对一映射,这实际上并不是如何使用接口的意图。

No, there is no concept of implementation and header files in C# like you find in C/C++. The closest you can come to this is to use an interface, but the interface can only define the public members of your class. You would then end up with a 1-to-1 mapping of classes and interfaces, which really isn't the intent for how interfaces are to be used.

埋葬我深情 2024-08-13 20:43:52

通过为每个类定义一个接口,然后让它们实现,您可以获得类似的结果。

You could get a similar result by defining an interface for each of your classes which they then implement.

江湖彼岸 2024-08-13 20:43:52

听起来您指的是接口 。在 C# 中,您可以在接口中定义所有成员函数,然后在另一个类中实现它们。

It sounds like you're referring to interfaces. In c#, you can define all of your member functions in an interface, and then implement them in another class.

笔芯 2024-08-13 20:43:52

在 C# 中,您可以在一定程度上使用部分类和部分成员来伪造它,但是,前向声明和原型在您的新语言中就像渡渡鸟一样。类视图、类图、智能感知等都有助于消除对这些“功能”的潜在需求。

In C# you could fake it with partial classes and partial members to a point, however, forward declarations and prototypes go the way of the dodo bird with your newer languages. Class View, Class Diagrams, Intellisense, et al, all help to remove the potential need for those "features".

幸福%小乖 2024-08-13 20:43:52

定义一个接口。

然后,能够使用良好的代码辅助工具自动实现该接口真是太好了。

Define an interface.

Then it's nice to be able to automatically implement the interface using a nice code assist tool.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文