using System; using System.Runtime.InteropServices; namespace TimeTest { class Program { static void Main(string[] args) { SYSTEMTIME t; GetLocalTime(out t); Console.WriteLine($"{t.wYear}/{t.wMonth}/{t.wDay} {t.wHour}:{t.wMinute}:{t.wSecond}"); } [DllImport("kernel32.dll")] static extern void GetLocalTime(out SYSTEMTIME lpSystemTime); } [StructLayout(LayoutKind.Sequential, Pack = 2)] struct SYSTEMTIME { public ushort wYear; public ushort wMonth; public ushort wDayOfWeek; public ushort wDay; public ushort wHour; public ushort wMinute; public ushort wSecond; public ushort wMilliseconds; } } 

When compiled, it gives an error in the code: Console.WriteLine - Unexpected $

What is its feature?

And how to use it?

Currently tested on Net.4.0

  • 3
    C # 6 (11 characters needed ...) - Igor
  • @ArteS Use @ instead of $. This character is used to declare "literal" string literals. - Vlad from Moscow
  • Yes, I just often meet the $ sign, so I decided to find out what it is eaten with, thank you for what they explained) - GooliveR
  • 2
    @VladfromMoscow He also seems to need an interpolated string - Igor
  • one
    The .NET version does not matter. Available in C # 6 => VS2015 and up. In the project build settings, the standard should be C # 6 or higher - rdorn

1 answer 1

This innovation of C # 6 is called "string interpolation".

This symbol allows you to specify variables surrounded by curly brackets directly in strings without using concatenation or formatting.

For example, instead of writing:

 Console.WriteLine(String.Format("{0}/{1}/{2} {3}:{4}:{5}", t.wYear, t.wMonth, t.wDay, t.wHour, t.wMinute, t.wSecond)); 

or

 Console.WriteLine(t.wYear + "/" + t.wMonth + "/" + t.wDay + " " + t.wHour + ":" + t.wMinute + ":" + t.wSecond); 

Much more readable looks like this:

 Console.WriteLine($"{t.wYear}/{t.wMonth}/{t.wDay} {t.wHour}:{t.wMinute}:{t.wSecond}"); 

Interpolation of lines was introduced in version C # 6.

If you have this entry fails the syntax check, try upgrading to Visual Studio 2015.

I also recommend reading the article on MSDN: https://msdn.microsoft.com/ru-ru/library/dn961160.aspx