[英]How can I declare a method then define it later?
In C# is there a way to declare the class then define it later? 在C#中有一种方法可以声明该类,然后再定义它吗? I really like in C++ where I can list all the methods at the top like a TOC then define everything later. 我非常喜欢在C ++中,我可以像TOC一样列出顶部的所有方法,然后再定义所有方法。
Can that be done is C#? 可以这样做是C#?
I have used the idea of defining a method that just runs a similarly named method in it then the similar method is at the bottom. 我已经使用了定义一个方法的想法,该方法只是在其中运行一个类似命名的方法,然后类似的方法在底部。 but I am thinking there is a better way and googling returns a bunch of basic code on creating classes with no answer. 但我认为有更好的方法和谷歌搜索返回一堆基本代码创建没有答案的类。
so here is what I do... 所以这就是我做的......
...
public void methodA(){methodAcontent()};
public void methodB()...etc...
...further down...
private void methodAcontent(){
...All the code..
}
is there a better way? 有没有更好的办法?
Why would you need that? 你为什么需要那个?
C# is using multipass compilation, so it doesn't matter where the function is defined and when used. C#正在使用多通道编译,因此定义函数的位置和使用时无关紧要。 You can have function defined at end of the class and use it in the beginning and it will still compile fine. 您可以在类的末尾定义函数并在开头使用它,它仍然可以正常编译。
Also IDE helps you with that. IDE也可以帮助您。 You have ability to collapse bodies of all methods, there is list of all methods in one combobox and InteliSense is extremly helpful in finding correct methods. 你有能力折叠所有方法的主体,在一个组合框中有所有方法的列表,InteliSense在寻找正确方法方面极为有用。
And using practices from C++ in C# is really bad idea, because both are quite different in how they solve the problems. 在C#中使用C ++中的实践是非常糟糕的主意,因为两者在解决问题方面都有很大不同。
abstract
方法。 There's not a good way to do this in the C# language, but the Visual Studio IDE can collapse a file to its definitions which you can then expand individually (see this ). 在C#语言中没有一种好方法可以做到这一点,但Visual Studio IDE可以将文件折叠到其定义,然后您可以单独扩展(请参阅此内容 )。 This along with code regions helps me organize longer files. 这与代码区域一起帮助我组织更长的文件。
If you're doing this as a means to "document" the public interface to a class that's properly encapsulating a concept or object in your problem domain, then use an interface. 如果您将此作为一种方法将公共接口“记录”到正确封装问题域中的概念或对象的类,则使用接口。
If you're doing it as a means to get an "overview" the structure of a class, then Visual Studio has several ways to give you this. 如果你这样做是为了获得一个“概述”类的结构,那么Visual Studio有几种方法可以解决这个问题。 You can collapse the code to just its definitions (Ctrl+M, O), or look at the Class View (Ctrl+W, C). 您可以将代码折叠到其定义(Ctrl + M,O),或查看类视图(Ctrl + W,C)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.