为什么 Visual Studio 对 C# 和 C++ 使用不同的缩进设置?

发布于 2024-07-14 06:36:38 字数 152 浏览 3 评论 0原文

对于 C#,Visual Studio 默认使用 4 个空格,而对于 C++,它是硬制表符。 为什么会这样呢? 为什么不一样呢?

我的项目由 C# 和 C++ 代码组成,这种差异确实让我很恼火。 我想为所有来源设定一个通用标准,但我想知道这是否会有任何缺点。

For C# Visual Studio uses 4 spaces by default, whereas for C++ it is hard-tabs. Why is it so? Why is it different?

My project consists of both C# and C++ code and the difference really annoys me. I want to set a common standard for all the sources, but I wonder if this would have any drawbacks.

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

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

发布评论

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

评论(8

天冷不及心凉 2024-07-21 06:36:38

如果您正在为这种差异寻找完全合乎逻辑、合理的理由,我认为您找不到。 尽管围绕制表符与空格存在许多激烈的战争,但实际上并没有真正的赢家(否则战争就会结束)。

解决这个问题的方法是通过 .vssettings 文件。 当我第一次开始使用 VS 2005 时,我为每种语言设置了默认制表符/空格。 您可以通过“工具”->“工具”来完成此操作。 选项-> 文本编辑器 -> (C#/C++)。 然后我将选项卡设置导出到 .vsssettings 文件中。 每当我在新机器上安装 VS 时,我只需导入该文件,然后我就有了满意的空间/选项卡设置。

PS:空格规则:)

If you're looking for a completely logical, well reasoned justification for this difference I don't think you'll find one. Despite the many flame wars around tabs vs spaces there really isn't a real winner (otherwise the war would be over).

The way to get around this is via a .vssettings file. When I first started with VS 2005, I set the tabs/spaces default in each language. You can do this via Tools -> Options -> Text Editor -> (C#/C++). Then I export the tab settings into a .vsssettings file. Whenever I install VS on a new machine I just import that file and I have my happy space/tab settings.

PS: spaces rule :)

妖妓 2024-07-21 06:36:38

当然 tabs 是唯一正确的方法。 Tab 可以做空格可以做的一切。 空格无法完成制表符的大部分功能。

选项卡可以轻松更改缩进和轻松导航,而无需多次按箭头键,并且必须保持控制。

真正的解决方案是让编辑器能够导航和制表符缩进文件,就好像它是空格缩进一样,如果用户愿意的话。 该文件将用选项卡保存。 非常简单并且包容每个人。

c# 2010 中的问题是我找不到任何选项将其更改为选项卡。

Of course tabs is the only right way. Tab can do everything spaces can do. Spaces cannot do much of what tab can do.

Tabs allow easy change of indentation and easy navigation without having to press arrow keys too many times, and would having to hold control.

The real solution is to have the editor be able to navigate and tab indented file as if it was space indented, if the user prefers so. The file would be saved with tabs. Very simple and accommodating everybody.

The problem in c# 2010 is that I cannot find any option to change it to tabs.

白衬杉格子梦 2024-07-21 06:36:38

我怀疑 C++ 语法不区分制表符和空格,因此存在任何缺点。
顺便说一句,我认为设置代码风格标准的最佳方法是导出 VS 设置并与团队共享。

I doubt there are any drawbacks as C++ grammar doesn't distinguish between tabs and spaces.
By the way, I think the best way to set code style standards is to export VS settings and share it with the team.

晨敛清荷 2024-07-21 06:36:38

C# 默认 4 个空格的原因是,无论谁打开文件,缩进都是相同的。

您可以在首选项中随意更改它。

The reason behind C#'s default 4 spaces is that whoever will open the file the indentation will be the same.

You are free to change it as you like in preferences.

冷︶言冷语的世界 2024-07-21 06:36:38

你喜欢什么就用什么,我通常在 C++ 中使用制表符。 无论如何,那些查看我的代码的人都使用默认的 VS 设置 RE 选项卡,因此没有真正的吸引力将其替换为空格。 我不确定现在是什么样子,但是当 F# 处于起步阶段(即 MSR 的早期版本)时,您需要在 #light 代码中使用空格,否则编译器会抱怨空格很重要。

不同的人有不同的偏好,我知道有些人虔诚地使用空格,而另一方面我知道更多人使用制表符。

Just go with whatever you prefer, I normally use tabs in C++. Those who view my code all use the default VS settings RE tabs anyway so there is no real pull to replace that with spaces. I'm not sure what its like now but when F# was in its infancy (i.e., pretty early release out of MSR) you needed to use spaces in #light code otherwise the compiler would complain as whitespace was important.

Different people have different preferences, I know some who religiously use spaces, on the other hand I know more that uses tabs.

羁绊已千年 2024-07-21 06:36:38

空格使使用键盘浏览代码变得更加困难。

对于空格,大多数人会使用四个空格,但有些人会使用两个或三个空格。 使用制表符,那些希望代码中包含两个空格的人可以简单地将制表符设置为显示与两个空格一样宽,并且编码风格保持一致。

如果您在任何 HTML、CSS 或 JavaScript 中使用空格作为制表符,那么您就错了。 html/css/js 没有制表符与空格之争——要么是制表符,要么是无知。 客户端代码未编译,并且 IIS 默认情况下未打开压缩 - 多余的空白 = 多余的脂肪。

很久以前,VB6 并没有让我们选择使用制表符,但现在我们可以而且应该 - 如果您不这样做,您的代码会让您看起来仍然坚持老式的方式。

所以别再像 1999 年那样编码了。使用制表符。 为孩子们做吧。

Spaces make it more difficult to navigate through code with the keyboard.

With spaces, most people will use four spaces, but some will use two or three. With tabs, the people who want their code tabbed out with two spaces can simply set the tab character to display as wide as two spaces, and the coding style remains consistent.

If you're using spaces for tabs in any HTML, CSS, or JavaScript, you're doing it wrong. There's no tabs-vs-spaces debate for html/css/js - it's either tabs, or ignorance. Client-side code is not compiled, and IIS doesn't have compression turned on by default - excess white space = excess fat.

Way back when, VB6 didn't give us the choice to use tabs, but now we can and should - if you don't, your code makes it look like you're still clinging to old-school ways.

So stop coding like it's 1999. Use tabs. Do it for the children.

魔法少女 2024-07-21 06:36:38

<块引用>

对于 C#,Visual Studio 默认使用 4 个空格,而对于 C++,它是硬制表符。 为什么会这样?

为什么不?

如果你不高兴就改变它。

For C# Visual Studio uses 4 spaces by default, whereas for C++ it is hard-tabs. Why is it so?

Why not?

Just change it if you're not happy.

╰沐子 2024-07-21 06:36:38

制表符与空格的争论有一个简单的答案:制表符。 制表符允许您指定缩进,而不强迫其他人接受它。 鉴于选项卡大小仅仅是个人偏好,用户不应该能够强迫其他人遵循他/她的显示选项。

为什么 C++ 和 C# 之间存在差异:不知道。 然而,知道选项卡在逻辑上是更好的选择,只需将 C#(实际上是所有语言)更改为选项卡即可。

The tabs vs spaces argument has a simple answer: tabs. Tabs allow you to specify your indentation while not forcing others to live with it. Given that tab sizes are merely a personal preference, one user shouldn't be able to force others to follow his/her display options.

Why the difference between C++ and C#: no clue. However, knowing that tabs are logically a better option, just change the C# (and in fact, all languages) to tabs and voila.

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