如何将这些变量写入 C# 中的一行代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15418467/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:45:52  来源:igfitidea点击:

How can I write these variables into one line of code in C#?

c#consoleintvarconsole.writeline

提问by Erik Grosskurth

I am new to C#, literally on page 50, and i am curious as to how to write these variables in one line of code:

我是 C# 的新手,字面上是第 50 页,我很好奇如何在一行代码中编写这些变量:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace consoleHelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {

            int mon = DateTime.Today.Month;
            int da = DateTime.Today.Day;
            int yer = DateTime.Today.Year;
            var time = DateTime.Now;

            Console.Write(mon);
            Console.Write("." + da);
            Console.WriteLine("." + yer);
        }
    }
}

I am coming from JavaScript where to do this it would look like this:

我来自 JavaScript,在那里执行此操作它看起来像这样:

document.write(mon+'.'+da+'.'+yer);

Any help here is appreciated.

任何帮助在这里表示赞赏。

采纳答案by Jim Mischel

Look into composite formatting:

查看复合格式

Console.WriteLine("{0}.{1}.{2}", mon, da, yer);

You could also write (although it's not really recommended):

你也可以写(虽然不是真的推荐):

Console.WriteLine(mon + "." + da + "." + yer);

And, with the release of C# 6.0, you have string interpolation expressions:

而且,随着 C# 6.0 的发布,您有了字符串插值表达式:

Console.WriteLine($"{mon}.{da}.{yer}");  // note the $ prefix.

回答by mattytommo

You can do pretty much the same as in JavaScript. Try this:

您可以执行与 JavaScript 几乎相同的操作。尝试这个:

Console.WriteLine(mon + "." + da + "." + yer);

Or you can use WriteLineas if it were a string.Formatstatement by doing:

或者您可以通过执行以下操作将WriteLine其用作string.Format语句:

Console.WriteLine("{0}.{1}.{2}", mon, da, yer);

which is equivalent to:

这相当于:

string.Format("{0}.{1}.{2}", mon, da, yer);

The number of parameters can be infinite, just make sure you correctly index those numbers (starting at 0).

参数的数量可以是无限的,只要确保正确索引这些数字(从 0 开始)。

回答by Mike C.

Give this a go:

试一试:

string format = "{0} / {1} / {2} {3}";
string date = string.Format(format,mon.ToString(),da.ToString(),yer.ToString();
Console.WriteLine(date);

In fact, there's probably a way to format it automatically without even doing it yourself.

事实上,可能有一种方法可以自动格式化它,而无需自己动手。

Check out http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

查看http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

回答by Justin Niessner

If you want to use something similar to the JavaScript, you just need to convert to strings first:

如果你想使用类似于 JavaScript 的东西,你只需要先转换为字符串:

Console.WriteLine(mon.ToString() + "." + da.ToString() + "." + yer.ToString());

But a (much) better way would be to use the format option:

但是(好多)更好的方法是使用 format 选项:

Console.WriteLine("{0}.{1}.{2}", mon, da, yer);

回答by Simon

You should try this one:

你应该试试这个:

Console.WriteLine("{0}.{1}.{2}", mon, da, yet);

See http://www.dotnetperls.com/console-writelinefor more details.

有关更多详细信息,请参阅http://www.dotnetperls.com/console-writeline

回答by Malcolm O'Hare

You can do your whole program in one line! Yes, that is right, one line!

您可以在一行中完成整个程序!是的,没错,一行!

Console.WriteLine(DateTime.Now.ToString("yyyy.MM.dd"));

You may notice that I did not use the same date format as you. That is because you should use the International Date Formatas described in this W3C document. Every time you don't use it, somewhere a cute little animal dies.

您可能会注意到我没有使用与您相同的日期格式。这是因为您应该使用此 W3C 文档中所述的国际日期格式。每次你不使用它,就会有一只可爱的小动物死去。

回答by phillk6751

Simple as:

简单如:

DateTime.Now.ToString("MM.dd.yyyy");

link to MSDN on ALL formatting options for DateTime.ToString() method

有关 DateTime.ToString() 方法的所有格式选项的 MSDN 链接

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx

回答by Apollo SOFTWARE

You could theoretically do the entire thing as simply:

从理论上讲,您可以简单地完成整个事情:

using System;
using System.Collections.Generic;
using System.Linq; 
using System.Text;

namespace consoleHelloWorld {
class Program {
    static void Main(string[] args) {
       Console.WriteLine(DateTime.Now.ToString("MM.dd.yyyy"));
    }
  }
}

回答by dhelvana25

 DateTime dateTime = dateTime.Today.ToString("MM.dd.yyyy");

 Console.Write(dateTime);

回答by piro_gert

Use $ before " " it will allow to write variables between these brackets

在“”之前使用 $ 它将允许在这些括号之间写入变量

 Console.WriteLine($"{mon}.{da}.{yer}");

The pro way :

专业方法:

  Console.WriteLine($"{DateTime.Today.Month}.{DateTime.Today.Day}.{DateTime.Today.Year}");
  Console.WriteLine($"month{DateTime.Today.Month} day{DateTime.Today.Day} year{DateTime.Today.Year}");

5.24.2016

2016 年 5 月 24 日

month5 day24 year2016

月 5 日 24 年 2016