使用Environment.NewLine别名 [英] Using an alias for Environment.NewLine

查看:381
本文介绍了使用Environment.NewLine别名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前最好的做法是使用Environment.NewLine在代码中,好了,开始一个新行。我希望能够使用别名或重载操作在我的代码,使其更加简洁。
取而代之的是:

Current best practice is to use Environment.NewLine in your code to, well, start a new line. I would like to be able to use an alias or overloaded operator in my code so that it is more concise. Instead of this:

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



我想有这样的事情:

I would like to have something like this:

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



我怎样才能轻松地设置这一次作为一个IDE设置,或为整个项目/命名空间?

How can I easily set this up one time as an IDE setting, or for a whole project/namespace?

这是别名或重载运算符是想到的,但不知道是否有做一个全球性的别名比Environment.NewLine更简洁的一个很好的方式,和我从来没有做过一个重载运算符之前,因此不熟悉的来龙去脉。

An alias or overloaded operator is that comes to mind, but not sure if there is a good way of doing a global alias that is more concise than Environment.NewLine, and I've never done an overloaded operator before, so not familiar with the ins and outs of that.

推荐答案

简单的方法缩短。弹出这个类的实用组件之一:

Simple shortening method. Pop this class in one of your utility assemblies:

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



然后就可以愉快地使用它作为这样的:

then you can happily use it as such:

using MyCompany;

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

这篇关于使用Environment.NewLine别名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆