【问题标题】:Using an alias for Environment.NewLine使用 Environment.NewLine 的别名
【发布时间】:2019-01-28 22:14:10
【问题描述】:

当前的最佳做法是在您的代码中使用 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 设置或整个项目/命名空间?

想到了别名或重载运算符,但不确定是否有比 Environment.NewLine 更简洁的全局别名的好方法,而且我以前从未做过重载运算符,所以没有熟悉其中的来龙去脉。

【问题讨论】:

  • 为什么不直接使用字符串常量,给它赋值Environment.NewLine
  • 给我一个例子。假设是一个WinForms项目,是否可以设置一次,然后在整个项目中使用?

标签: c#


【解决方案1】:

简单的缩短方法。在您的一个实用程序程序集中弹出此类:

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");

【讨论】:

  • 我已经接受这是最接近我正在寻找的东西。但是,这里有很多有价值的输入。感谢所有参与的人。
【解决方案2】:

我可以建议您改用扩展方法吗?

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 -- 不过可能不清楚。

【讨论】:

  • 这是一个创造性的解决方案,尽管生成的代码并不像我想要的那样简单/优雅。感谢您提供它。
  • 这是method chainingfluent interface 方法。
【解决方案3】:

Environment.NewLine 很少的情况下使用StringBuilder.AppendLine()

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

【讨论】:

    【解决方案4】:

    编写一个类,提供Environment.NewLine的值作为成员,如Jesse C. Slicer has already suggested

    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

    【讨论】:

      【解决方案5】:
      using static System.Environment;
      

      那你就可以直接用NewLine

      【讨论】:

      【解决方案6】:

      别名不起作用 - 您可以为命名空间或类型设置别名,但不能为类型的属性设置别名。所以这行得通:

      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 的字符串连接,那么绝对值得痛苦......

      【讨论】:

        【解决方案7】:

        添加到@abatishchev 响应中,您可以使用 StringBuilder 类做一些好事。

        StringBuilder builder = new StringBuilder();
        
        builder.Append("List:");
        
        builder.AppendLine();
        
        builder.Append("1. Boat")
        
        builder.Append("2. Car").AppendLine();
        
        builder.Replace("Boat", "Jet");
        

        【讨论】:

          猜你喜欢
          • 2010-11-04
          • 2011-06-27
          • 1970-01-01
          • 1970-01-01
          • 2018-03-30
          • 1970-01-01
          • 2019-10-11
          • 2023-02-22
          相关资源
          最近更新 更多