DateTime c# is predefined? but only in Rider?

The book C# 12 in a Nutshell and this SO post clearly say:

The System namespace in the .NET Framework contains many important
types that are not predefined by C# e.g. DateTime.

However, running those very two lines in JetBrains Rider works

DateTime now = DateTime.Now;
Console.WriteLine(now);

enter image description here

This does not work on online websites to run C# code (like https://rextester.com/ or https://www.programiz.com/csharp-programming/online-compiler/).

Why is there a difference? It seems the using System is there by default in Rider?

  • I know this is not vital in the day-to-day life of a Software Engineer but I’m still wondering why there is a difference to see how things really work “under the hood”

    – 

  • 2

    This works just fine on said website – unless you also removed the using System; at the top, as what you’re typing in Rider is a “Top-level statements”-style Program.cs (learn.microsoft.com/en-us/dotnet/csharp/fundamentals/…) with implicit usings (devblogs.microsoft.com/dotnet/welcome-to-csharp-10) automatically importing the System namespace for you.

    – 

  • I had removed the using System; at the top yes to mimic what I was seeing in Rider. Turns out the ImplicitUsings were putting them back. Thanks!

    – 




  • Does this answer your question? Where are the using statements/directives in .NET 6 and above

    – 

  • Yes it does I had been on it but gave up because I was not understanding anything but now that I’m re-reading it it did! (I’m a beginner in programming so I realised too late I guess)

    – 

The project is likely using Implicit Usings. This is a feature of the SDK that will, as its name suggests, implicitly include using statements for the most common namespaces of the project type. To check if this feature is enabled, see if this is included in the .csproj file:

<ImplicitUsings>enable</ImplicitUsings>

The base SDK will include the following namespaces implicity with this feature:

System
System.Collections.Generic
System.IO
System.Linq
System.Net.Http
System.Threading
System.Threading.Tasks

See: https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview#implicit-using-directives

Leave a Comment