Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert to interpolated strings (VS_Snippets_CLR directory) #45433

Merged
merged 4 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ title: "Custom date and time format strings"
description: Learn to use custom date and time format strings to convert DateTime or DateTimeOffset values into text representations, or to parse strings for dates & times.
ms.date: 05/12/2022
ms.topic: reference
dev_langs:
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
helpviewer_keywords:
- "formatting [.NET], dates"
- "custom DateTime format string"
- "format specifiers, custom date and time"
Expand Down Expand Up @@ -715,9 +715,9 @@ The following example includes the literal characters "pst" (for Pacific Standar

A custom date and time format string consists of two or more characters. Date and time formatting methods interpret any single-character string as a standard date and time format string. If they don't recognize the character as a valid format specifier, they throw a <xref:System.FormatException>. For example, a format string that consists only of the specifier "h" is interpreted as a standard date and time format string. However, in this particular case, an exception is thrown because there is no "h" standard date and time format specifier.

To use any of the custom date and time format specifiers as the only specifier in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format specifier by itself), include a space before or after the specifier, or include a percent ("%") format specifier before the single custom date and time specifier.
To use any of the custom date and time format specifiers as the only specifier in a format string (that is, to use the "d", "f", "F", "g", "h", "H", "K", "m", "M", "s", "t", "y", "z", ":", or "/" custom format specifier by itself), include a space before the specifier, or include a percent ("%") format specifier before the single custom date and time specifier.

For example, "`%h"` is interpreted as a custom date and time format string that displays the hour represented by the current date and time value. You can also use the " h" or "h " format string, although this includes a space in the result string along with the hour. The following example illustrates these three format strings.
For example, "`%h"` is interpreted as a custom date and time format string that displays the hour represented by the current date and time value. You can also use the " h" format string, although this includes a space in the result string along with the hour. The following example illustrates these format strings.

[!code-csharp-interactive[Formatting.DateAndTime.Custom#16](~/samples/snippets/csharp/VS_Snippets_CLR/Formatting.DateAndTime.Custom/cs/literal1.cs#16)]
[!code-vb[Formatting.DateAndTime.Custom#16](~/samples/snippets/visualbasic/VS_Snippets_CLR/Formatting.DateAndTime.Custom/vb/literal1.vb#16)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class AsyncMain
public class AsyncMain3
{
public static void Main()
{
Expand All @@ -22,8 +22,7 @@ public static void Main()
out threadId, null, null);

Thread.Sleep(0);
Console.WriteLine("Main thread {0} does some work.",
Thread.CurrentThread.ManagedThreadId);
Console.WriteLine($"Main thread {Thread.CurrentThread.ManagedThreadId} does some work.");

// Call EndInvoke to wait for the asynchronous call to complete,
// and to retrieve the results.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFramework>net481</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class AsyncMain
public class AsyncMain4
{
static void Main()
{
Expand Down Expand Up @@ -34,8 +34,7 @@ static void Main()
new AsyncCallback(CallbackMethod),
"The call executed on thread {0}, with return value \"{1}\".");

Console.WriteLine("The main thread {0} continues to execute...",
Thread.CurrentThread.ManagedThreadId);
Console.WriteLine($"The main thread {Thread.CurrentThread.ManagedThreadId} continues to execute...");

// The callback is made on a ThreadPool thread. ThreadPool threads
// are background threads, which do not keep the application running
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Examples.AdvancedProgramming.AsynchronousOperations
{
public class AsyncMain
public class AsyncMain2
{
static void Main()
{
Expand All @@ -22,8 +22,7 @@ static void Main()
out threadId, null, null);

Thread.Sleep(0);
Console.WriteLine("Main thread {0} does some work.",
Thread.CurrentThread.ManagedThreadId);
Console.WriteLine($"Main thread {Thread.CurrentThread.ManagedThreadId} does some work.");

// Wait for the WaitHandle to become signaled.
result.AsyncWaitHandle.WaitOne();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet4>
// <Snippet4>

Check failure on line 1 in samples/snippets/csharp/VS_Snippets_CLR/AsyncDesignPattern/CS/AsyncDelegateNoStateObject.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computers.
Expand All @@ -24,7 +24,7 @@
{
// Print a message to indicate that the application
// is still working on the remaining requests.
Console.WriteLine("{0} requests remaining.", requestCounter);
Console.WriteLine($"{requestCounter} requests remaining.");
}
public static void Main()
{
Expand Down Expand Up @@ -58,8 +58,7 @@
// A SocketException was thrown.
if (message != null)
{
Console.WriteLine("Request for {0} returned message: {1}",
hostNames[i], message);
Console.WriteLine($"Request for {hostNames[i]} returned message: {message}");
continue;
}
// Get the results.
Expand All @@ -68,15 +67,15 @@
IPAddress[] addresses = h.AddressList;
if (aliases.Length > 0)
{
Console.WriteLine("Aliases for {0}", hostNames[i]);
Console.WriteLine($"Aliases for {hostNames[i]}");
for (int j = 0; j < aliases.Length; j++)
{
Console.WriteLine("{0}", aliases[j]);
Console.WriteLine($"{aliases[j]}");
}
}
if (addresses.Length > 0)
{
Console.WriteLine("Addresses for {0}", hostNames[i]);
Console.WriteLine($"Addresses for {hostNames[i]}");
for (int k = 0; k < addresses.Length; k++)
{
Console.WriteLine("{0}",addresses[k].ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet5>
// <Snippet5>

Check failure on line 1 in samples/snippets/csharp/VS_Snippets_CLR/AsyncDesignPattern/CS/AsyncDelegateWithStateObject.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computer.
Expand Down Expand Up @@ -70,7 +70,7 @@
{
// Print a message to indicate that the application
// is still working on the remaining requests.
Console.WriteLine("{0} requests remaining.", requestCounter);
Console.WriteLine($"{requestCounter} requests remaining.");
}
public static void Main()
{
Expand Down Expand Up @@ -104,8 +104,7 @@
{
if (r.ExceptionObject != null)
{
Console.WriteLine("Request for host {0} returned the following error: {1}.",
r.HostName, r.ExceptionObject.Message);
Console.WriteLine($"Request for host {r.HostName} returned the following error: {r.ExceptionObject.Message}.");
}
else
{
Expand All @@ -115,15 +114,15 @@
IPAddress[] addresses = h.AddressList;
if (aliases.Length > 0)
{
Console.WriteLine("Aliases for {0}", r.HostName);
Console.WriteLine($"Aliases for {r.HostName}");
for (int j = 0; j < aliases.Length; j++)
{
Console.WriteLine("{0}", aliases[j]);
Console.WriteLine($"{aliases[j]}");
}
}
if (addresses.Length > 0)
{
Console.WriteLine("Addresses for {0}", r.HostName);
Console.WriteLine($"Addresses for {r.HostName}");
for (int k = 0; k < addresses.Length; k++)
{
Console.WriteLine("{0}",addresses[k].ToString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// AsynchSampler
// AsynchSampler

Check failure on line 1 in samples/snippets/csharp/VS_Snippets_CLR/AsyncDesignPattern/CS/Async_EndBlock.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
//<Snippet1>
/*
The following example demonstrates using asynchronous methods to
Expand Down Expand Up @@ -39,7 +39,7 @@
Console.WriteLine("Aliases");
for (int i = 0; i < aliases.Length; i++)
{
Console.WriteLine("{0}", aliases[i]);
Console.WriteLine($"{aliases[i]}");
}
}
if (addresses.Length > 0)
Expand All @@ -53,7 +53,7 @@
}
catch (SocketException e)
{
Console.WriteLine("An exception occurred while processing the request: {0}", e.Message);
Console.WriteLine($"An exception occurred while processing the request: {e.Message}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet2>
// <Snippet2>

Check failure on line 1 in samples/snippets/csharp/VS_Snippets_CLR/AsyncDesignPattern/CS/Async_EndBlockWait.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computer.
Expand Down Expand Up @@ -40,7 +40,7 @@
Console.WriteLine("Aliases");
for (int i = 0; i < aliases.Length; i++)
{
Console.WriteLine("{0}", aliases[i]);
Console.WriteLine($"{aliases[i]}");
}
}
if (addresses.Length > 0)
Expand All @@ -54,8 +54,7 @@
}
catch (SocketException e)
{
Console.WriteLine("Exception occurred while processing the request: {0}",
e.Message);
Console.WriteLine($"Exception occurred while processing the request: {e.Message}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<Snippet3>
//<Snippet3>

Check failure on line 1 in samples/snippets/csharp/VS_Snippets_CLR/AsyncDesignPattern/CS/Async_Poll.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
/*
The following example demonstrates using asynchronous methods to
get Domain Name System information for the specified host computer.
Expand Down Expand Up @@ -52,7 +52,7 @@
Console.WriteLine("Aliases");
for (int i = 0; i < aliases.Length; i++)
{
Console.WriteLine("{0}", aliases[i]);
Console.WriteLine($"{aliases[i]}");
}
}
if (addresses.Length > 0)
Expand All @@ -66,7 +66,7 @@
}
catch (SocketException e)
{
Console.WriteLine("An exception occurred while processing the request: {0}", e.Message);
Console.WriteLine($"An exception occurred while processing the request: {e.Message}");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//<snippet20>
//<snippet20>

Check failure on line 1 in samples/snippets/csharp/VS_Snippets_CLR/CodeDomExample/CS/source3.cs

View workflow job for this annotation

GitHub Actions / snippets-build

ERROR: Project missing. A project (and optionally a solution file) must be in this directory or one of the parent directories to validate and build this code.
using System;
using System.IO;
using System.CodeDom;
Expand All @@ -15,7 +15,7 @@
CodeCompileUnit codeUnit = new CodeCompileUnit();
sourcefile = GenerateCSharpCode(codeUnit);
exefile = sourcefile.Substring(0, sourcefile.LastIndexOf('.')) + ".exe";
Console.WriteLine("outfile: {0}", exefile);
Console.WriteLine($"outfile: {exefile}");
CompileCSharpCode(sourcefile, exefile);
}

Expand Down Expand Up @@ -82,8 +82,7 @@
if (cr.Errors.Count > 0)
{
// Display compilation errors.
Console.WriteLine("Errors building {0} into {1}",
sourceFile, cr.PathToAssembly);
Console.WriteLine($"Errors building {sourceFile} into {cr.PathToAssembly}");
foreach (CompilerError ce in cr.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
Expand All @@ -92,8 +91,7 @@
}
else
{
Console.WriteLine("Source {0} built into {1} successfully.",
sourceFile, cr.PathToAssembly);
Console.WriteLine($"Source {sourceFile} built into {cr.PathToAssembly} successfully.");
}

// Return the results of compilation.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <Snippet1>
// <Snippet1>
using System;
using System.Threading;

Expand All @@ -24,26 +24,21 @@ public static void Main()

private static void SleepIndefinitely()
{
Console.WriteLine("Thread '{0}' about to sleep indefinitely.",
Thread.CurrentThread.Name);
Console.WriteLine($"Thread '{Thread.CurrentThread.Name}' about to sleep indefinitely.");
try {
Thread.Sleep(Timeout.Infinite);
}
catch (ThreadInterruptedException) {
Console.WriteLine("Thread '{0}' awoken.",
Thread.CurrentThread.Name);
Console.WriteLine($"Thread '{Thread.CurrentThread.Name}' awoken.");
}
catch (ThreadAbortException) {
Console.WriteLine("Thread '{0}' aborted.",
Thread.CurrentThread.Name);
Console.WriteLine($"Thread '{Thread.CurrentThread.Name}' aborted.");
}
finally
{
Console.WriteLine("Thread '{0}' executing finally block.",
Thread.CurrentThread.Name);
Console.WriteLine($"Thread '{Thread.CurrentThread.Name}' executing finally block.");
}
Console.WriteLine("Thread '{0} finishing normal execution.",
Thread.CurrentThread.Name);
Console.WriteLine($"Thread '{Thread.CurrentThread.Name} finishing normal execution.");
Console.WriteLine();
}
}
Expand Down
Loading