使用别名代替Environment.NewLine

3

目前最佳实践是在代码中使用Environment.NewLine来开始新的一行。我希望能够在我的代码中使用别名或重载运算符,使其更加简洁。 而不是这样:

MessageBox.Show("My first line here" + Environment.NewLine + "My second line here");

我希望能有类似这样的内容:
MessageBox.Show("My first line here" + NL + "My second line here");

我该如何轻松地一次性设置IDE设置,或者针对整个项目/命名空间?一个别名或重载的运算符是我想到的,但不确定是否有更简洁的全局别名的好方法,而且我以前从未做过重载运算符,所以对其中的细节不熟悉。

2
为什么不直接使用字符串常量,将 Environment.NewLine 分配给它呢? - Arseni Mourzenko
给我一个例子。假设是一个WinForms项目,它可以被设置一次,然后在整个项目中使用吗? - Alvin S
7个回答

7

简单的缩短方法。将此类放入其中一个实用程序程序集中:

namespace MyCompany
{
    public static class E
    {
        public static readonly string NL = System.Environment.NewLine;
    }
}

那么你可以愉快地使用它,如下所示:
using MyCompany;

MessageBox.Show("My first line here" + E.NL + "My second line here");

我已经接受了这个最接近我所寻找的内容。然而,这里有很多有价值的输入。感谢所有参与者。 - Alvin S

4
using static System.Environment;

那么你可以直接使用它作为NewLine。最初的回答。

这是最优雅的解决方案。文档在此处 - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-static - Chris Koester

3
我可以建议您使用扩展方法来代替?
public static class StringExtensions
{
    public static string NextLine(this string s, string next)
    {
        return s + Environment.NewLine + next;
    }

    public static string NextLine(this string s)
    {
        // just add a new line with no text
        return s + Environment.NewLine;
    }
}

使用方法:

var lines = "My first line here".NextLine("My second line here.")
    .NextLine("third line").NextLine();

当然,如果你愿意,你可以称之为NL——虽然可能不太清晰。


2
这是一个有创意的解决方案,尽管最终的代码并不像我想要的那么简单/优美。感谢您提供它。 - Alvin S
这是一种“方法链式”或“流畅接口”的编程方式。 - DavidRR

2
编写一个类来提供Environment.NewLine的值作为成员,正如Jesse C. Slicer已经建议的那样:
namespace MyNamespace
{
    public static class Env
    {
        public static readonly string NL = Environment.NewLine;
    }
}

接下来,请添加以下using指令:

using E = MyNamespace.Env;

您可以将此 using 指令添加到您的默认新类模板和任何其他模板中(例如新的 struct、新的 interface 等)。
以下是我的机器上新类模板所在的位置,作为示例供您参考:
C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Code\1033
完成后,您就可以在任何想要的地方使用 E.NL 代替 Environment.NewLine

2

在含有较少的 Environment.NewLine 的情况下,使用 StringBuilder.AppendLine()

var sb = new StringBuilder();
sb.AppendLine("My first line here");
sb.AppendLine("My second line here");
MessageBox.Show(sb.ToString());

1

别名无法使用 - 你可以为命名空间或类型创建别名,但不能为类型的属性创建别名。所以这样是有效的:

using NL = System.Environment;

class Program
{
    static void Main(string[] args)
    {
        var s = NL.NewLine;
    }
}

但这不行:

// returns: The type name 'NewLine' does not 
// exist in the type 'System.Environment' error
using NL = System.Environment.NewLine;

重载运算符是一个有趣的想法,但是你将不得不使用除了String之外的其他东西。通常人们会创建一个struct,它可以接受一个基本字符串值,然后重载运算符。如果你只想替换Environment.NewLine,那么这并不值得付出这样的代价。最好像其他人建议的那样使用静态扩展。

另一种选择(如果你非要使用NL)是让你框架中的所有类都继承自一个共同的父类,该父类可以具有以下属性:

public class BaseParentClass
{
  public string NL
  {
    get { return System.Environment.NewLine; }
  }
}

然后在所有子类的代码中,你的代码将会简单地如下所示:

public class ChildOfBaseParent
{
  public void Show_A_Message()
  {
      MessageBox.Show("My first line here" + NL + "My second line here");
  }
}

当然,如果您的类没有共同的父类,您将不得不为了方便起见重构代码。您需要为WinForm类创建一个平行的System.Windows.Forms.Form父类以获得相同的行为。

但是,如果您有许多涉及NL的字符串连接,则绝对值得付出这种代价...


0

除了 @abatishchev 的回答,您还可以使用 StringBuilder 类来实现更多功能。

StringBuilder builder = new StringBuilder();

builder.Append("List:");

builder.AppendLine();

builder.Append("1. Boat")

builder.Append("2. Car").AppendLine();

builder.Replace("Boat", "Jet");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接