简体   繁体   English

我怎样才能声明一个方法,然后再定义它?

[英]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.

相关问题 如何在此方法中声明字符串? - How can I declare a string within this method? 如何定义通用扩展方法 - How can I define a generic extension method 如何声明类型列表,稍后实例化一个? - How do I declare a list of types, and later instantiate one? 将枚举声明为类属性(对于稍后定义的子类?) - Declare enum as a class property (for child classes to define later?) 如何在已初始化的对象上定义方法? - How can I define a method on an already initialized object? 如何在ChartWizard方法中定义系列标签? - How can I define series labels in ChartWizard method? 我如何使用方法异步任务 <string> 并返回字符串,然后如何将该方法的委托传递给构造函数并在以后使用它? - How can I use a method async Task<string> and return string, and then how do I pass a delegate for that method to a constructor and use it later? 如何为 IEnumerable 制作扩展方法<t> ? 我应该在哪里申报?</t> - How can I make an extension method for IEnumerable<T>? Where should I declare it? C#:如何在接口中声明一个方法以返回其类标记有[Serializable()]属性的对象? - C#: how can I declare a method in an interface to return an object whose class is marked with a [Serializable()] attribute? 我可以声明一个接口方法来返回C#中的通用接口吗? - Can I declare an interface method to return generic Interface in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM