Question

That's an easy one, but there is an error that appears. I have to create a console application for printing current date and time.

Here is my code:

namespace DateTime
{
    class DateTime
    {
        public static void Main()

        {
            DateTime now = DateTime.now;
            Console.WriteLine(now);
            Console.ReadLine();
        }
}
}

I'm using visual studio 2012.

Was it helpful?

Solution

There are a few issues here. First, your class is called DateTime, which is causing the compiler to confuse it with System.DateTime. Also make sure the System namespace is available with a using directive, or fully-qualifying the type. Thirdly, C# is case sensitive. It should be Now, not now. Try giving things different names:

using System;
namespace MyNamespace
{
    class MyClass
    {
        public static void Main()
        {
            DateTime now = DateTime.Now;
            Console.WriteLine(now);
            Console.ReadLine();
        }
    }
}

OTHER TIPS

A typo: DateTime.now. Now should start with the capital letter N as follows: DateTime.Now.

If you getting this error:

'System.DateTime' does not contain a definition for 'now'

Try DateTime.Now with uppercase 'N' instead DateTime.now.

Error 1 Invalid token '(' in class, struct, or interface member declaration c:\users\admin\documents\visual studio 2012\Projects\DateTime\DateTime\DateTime.cs 12 30 DateTime

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top