在C#中,当我有不同的代码段,如常量,API函数,辅助函数等时,我想将它们分开.我通常使用这样的东西:
public class Foo {
//================== Constants ==================
private const string VIP = "Cob H.";
private const int IMPORTANT_NUMBER = 23;
//================== API Functions ==================
[WebMethod(MessageName = "SomeInformation")]
public string SomeInformation() {
return VIP + " is dead.";
}
//================== Inner Classes ==================
private class IrrelevantClass {
public string Name { get; set; }
public string City { get; set; }
}
}
Run Code Online (Sandbox Code Playgroud)
是否有一种优雅的方式来划分它们而不是使用一堆丑陋的评论?就像在Objective-C中一样,你可以使用
#pragma mark - Inner Classes
Run Code Online (Sandbox Code Playgroud)
我查看了C#的pragma列表中的所有关键字,但没有一个看起来很有希望.
p.s*_*w.g 11
C#具有提供类似功能的区域.要使用区域,您的代码看起来有点像这样:
public class Foo {
#region Constants
private const string VIP = "Cob H.";
private const int IMPORTANT_NUMBER = 23;
#endregion
#region API Functions
[WebMethod(MessageName = "SomeInformation")]
public string SomeInformation() {
return VIP + " is dead.";
}
#endregion
#region Inner Classes
private class IrrelevantClass {
public string Name { get; set; }
public string City { get; set; }
}
#endregion
}
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Visual Studio,C#编辑器允许您折叠区域,从而更容易浏览大型源文件.
您可以使用#regions.
#region允许您指定在使用Visual Studio代码编辑器的大纲功能时可以展开或折叠的代码块.
public class Foo
{
#region Constants
private const string VIP = "Cob H.";
private const int IMPORTANT_NUMBER = 23;
#endregion
//......rest of the code
}
Run Code Online (Sandbox Code Playgroud)