C# 在 Visual Studio 2008 中打断长代码行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1263326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Break Long code lines in Visual Studio 2008
提问by muhan
In Visual Studio 2008, if I have a long line of code, how can i get that to breakup into multiple lines?
在 Visual Studio 2008 中,如果我有很长的代码行,我怎样才能把它分成多行?
public static void somemethod(param1, param2, param3, more params etc...)
How can I make this 1 line of code span 2 or 3 lines?
我怎样才能让这 1 行代码跨越 2 或 3 行?
采纳答案by Tim Booker
Hit the enter key.
按回车键。
public static somemethod(param1,
param2,
param3,
more params etc...)
...is perfectly valid.
...完全有效。
回答by scwagner
You mean how do you do it automatically, or by hand? There are some tools like Resharperthat have features to "wrap" long lines of code. If you want to do it manually, then just press the Enter key anywhere that's not in the middle of an identifier.
您的意思是如何自动或手动执行此操作?有一些像Resharper这样的工具具有“包装”长代码行的功能。如果您想手动执行此操作,只需在标识符中间以外的任何位置按 Enter 键即可。
回答by Hamish Smith
C# doesn't need any line continuation characters (the way basic does). Just insert a line break anywhere in the line.
C# 不需要任何行继续符(basic 的方式)。只需在行中的任意位置插入换行符。
public static somemethod(type param1,
type param2,
type param3)
{
}
works just fine.
工作得很好。
If you look at linq and fluent interface samples you will see some idiomatic ways to break long lines:
如果您查看 linq 和 fluent 界面示例,您会看到一些惯用的方式来打破长行:
builder
.AddSomething()
.If((z) => z.SomeCondition)
.AddSomethingElse();
回答by Sam Harwell
You have a couple options:
您有几个选择:
- Tools > Options > All Languages > General > Enable Word Wrap
- Use the following regex in the Find In Files dialog to find long lines (120 characters in this case) in the project so you can split them?
^.^120
- 工具 > 选项 > 所有语言 > 常规 > 启用自动换行
- 在“在文件中查找”对话框中使用以下正则表达式在项目中查找长行(在本例中为 120 个字符),以便您可以拆分它们?
^.^120
Edit: Seeing the marked answer - I rather assumed that part was known. :o
编辑:看到标记的答案 - 我宁愿假设那部分是已知的。:o
回答by Guffa
C# is not line based, so you can split the statements anywhere but inside an identifier:
C# 不是基于行的,因此您可以在标识符内的任何位置拆分语句:
public static void somemethod(
int param1,
int param2,
int param3,
more params etc...
)
You can even write something like:
你甚至可以这样写:
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
Console
.
WriteLine
(
i
)
;
}
回答by Guffa
To break strings you can place a _
at the break in VB.Net and in C# you put @
before the string.
要中断字符串,您可以_
在 VB.Net 和 C# 中放置@
在字符串之前的中断处。
Code in C#:
C#中的代码:
string s=@"sdsdsdsd
dfdfdfdfdf
fdfdfdfdf";
Code in VB
VB中的代码
s="fdfdfdfdfdf _
dfdfdfdfdfdf "