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:
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:
document.write(mon+'.'+da+'.'+yer);
Any help here is appreciated.
This question is related to
c#
console
int
var
console.writeline
If you want to use something similar to the JavaScript, you just need to convert to strings first:
Console.WriteLine(mon.ToString() + "." + da.ToString() + "." + yer.ToString());
But a (much) better way would be to use the format option:
Console.WriteLine("{0}.{1}.{2}", mon, da, yer);
Simple as:
DateTime.Now.ToString("MM.dd.yyyy");
link to MSDN on ALL formatting options for DateTime.ToString() method
You should try this one:
Console.WriteLine("{0}.{1}.{2}", mon, da, yet);
See http://www.dotnetperls.com/console-writeline for more details.
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 Format as described in this W3C document. Every time you don't use it, somewhere a cute little animal dies.
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
month5 day24 year2016
DateTime dateTime = dateTime.Today.ToString("MM.dd.yyyy");
Console.Write(dateTime);
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
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"));
}
}
}
You can do pretty much the same as in JavaScript. Try this:
Console.WriteLine(mon + "." + da + "." + yer);
Or you can use WriteLine
as if it were a string.Format
statement by doing:
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).
Source: Stackoverflow.com