Erik Grosskurth
Erik Grosskurth

Reputation: 3932

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

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.

Upvotes: 34

Views: 180020

Answers (10)

Jim Mischel
Jim Mischel

Reputation: 134065

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:

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

Upvotes: 73

piro_gert
piro_gert

Reputation: 11

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

Upvotes: 1

Simon
Simon

Reputation: 434

You should try this one:

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

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

Upvotes: 6

dhelvana25
dhelvana25

Reputation: 21

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

 Console.Write(dateTime);

Upvotes: 2

Mathew Thompson
Mathew Thompson

Reputation: 56449

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).

Upvotes: 9

C0L.PAN1C
C0L.PAN1C

Reputation: 12243

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

Upvotes: 1

Malcolm O'Hare
Malcolm O'Hare

Reputation: 5009

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.

Upvotes: 18

phillk6751
phillk6751

Reputation: 187

Simple as:

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

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

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

Upvotes: 0

Justin Niessner
Justin Niessner

Reputation: 245479

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

Upvotes: 5

Mike C.
Mike C.

Reputation: 3114

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

Upvotes: 0

Related Questions