gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:51:38 26 4
gpt4 key购买 nike

我是 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);
}
}
}

我来自 JavaScript,在哪里做它看起来像这样:

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

在此感谢任何帮助。

最佳答案

查看composite formatting :

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

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

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

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

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

关于c# - 如何将这些变量写入 C# 中的一行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15418467/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com