C# Embeddings
- 5 minutes to read
This topic lists available Selection Embeddings for C#.
try..catch
Embeds the selected text (code, comments) into the try..catch code block and places the caret in the catch section.
try
{
// Embedded text
}
catch (Exception ex)
{
│
}
You can also use the C shortcut for this embedding.[2]
try..finally
Embeds the selected text (code, comments) into the try..finally code block and places the caret in the finally section.
try
{
// Embedded text
}
finally
{ │
}
You can also use the F shortcut for this embedding.[2]
try..catch/finally
Embeds the selected text (code, comments) into the try..catch..finally code block. This embedding also places the caret in the catch section and drops a marker in the finally section.
try
{
// Embedded text
}
catch (Exception ex)
{
│
}
finally
{
}
You can also use the T shortcut for this embedding.[2]
#region..#endregion
Embeds the selected text (code, comments) between the #region and #endregion directives and selects the region name placeholder.
#region │RegionName
// Embedded text
#endregion
You can also use the Ctrl + 3 shortcut for this embedding.
#if..#endif
Embeds the selected text (code, comments) between the #if and #endif directives and places the caret after the #if directive.
#if │
// Embedded text
#endif
if ()
Embeds the selected text (code, comments) into the if conditional block, selects the conditional expression placeholder, and places the caret before this placeholder.
if (│Expression)
{
// Embedded text
}
if () else
Embeds the selected text (code, comments) into the if conditional block with the else branch. This embedding also selects the conditional expression placeholder and places the caret before this placeholder.
if (│Expression)
{
// Embedded text
}
else
{
}
while ()
Embeds the selected text (code, comments) into the while loop, selects the conditional expression placeholder, and places the caret before this placeholder.
while (│Expression)
{
// Embedded text
}
do..while ()
Embeds the selected text (code, comments) into the do..while loop, selects the conditional expression placeholder, and places the caret before this placeholder.
do
{
// Embedded text
} while (│Expression);
using ()
Embeds the selected text (code, comments) into the using () block, selects the object creation placeholder, and places the caret before this placeholder.
using (│Expression)
{
// Embedded text
}
lock ()
Embeds the selected text (code, comments) into the lock () block, selects the object placeholder, and places the caret before this placeholder.
lock (│Expression)
{
// Embedded text
}
block - {}
Embeds the selected text (code, comments) in braces and selects the generated block.
{
// Embedded text
}
You can also use the B shortcut for this embedding.[2]
Embeddings for Rich Comments
CodeRush includes embeddings that allow you to format selected text within comments. The following embeddings are available:
To use these embedding, enable the Rich Comments feature.
Bold
Formats the selected text within a comment as bold.
{
// **Bold** text
}
You can also use the Ctrl+B shortcut for this embedding.
Italic
Formats the selected text within a comment as italic.
{
// *Italic* text
}
You can also use the Ctrl+I shortcut for this embedding.
Underline
Formats the selected text within a comment as underline.
{
// _Underline_ text
}
You can also use the Ctrl+U shortcut for this embedding.
Strikethrough
Formats the selected text within a comment as underline.
{
// ~Strikethrough~ text
}
You can also use the Ctrl+S shortcut for this embedding. This shortcut is initially disabled. To enable it, refer to the Selection | Embedding group in the shortcuts configuration.
Hidden Embeddings in the Code Actions Menu
BeginUpdate..EndUpdate
Embeds the selected text (code, comments) between BeginUpdate and EndUpdate method calls of an object from the clipboard.
This embedding uses the «Paste» Text Command and requires an identifier in the clipboard.
«Paste».BeginUpdate();
try
{
// Embedded text
}
finally
{
«Paste».EndUpdate();
}
WaitCursor
Embeds the selected text (code, comments) into the try..finally block. The WaitCursor is used during the selected code lines’ execution.
This embedding also creates linked identifiers for references to the saveCursor variable. The System.Windows.Forms namespace should be referenced.
Cursor │saveCursor = Cursor.Current;
try
{
Cursor.Current = Cursors.WaitCursor;
// Embedded text
}
finally
{
Cursor.Current = saveCursor;
}
Stopwatch
This embedding performs the following actions:
Creates a Stopwatch instance of System.Diagnostics
Embeds the selected text (code, comments) into the try{} finally{} block
Creates linked identifiers for references to the stopwatch variable
Starts the stopwatch before the embedded text
Stops the stopwatch after the embedded text
Outputs the stopwatch.Elapsed value to the console
using System.Diagnostics;
Stopwatch │stopwatch = new Stopwatch();
stopwatch.Start();
try
{
// Embedded text
}
finally
{
stopwatch.Stop();
}
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);
To string
Quotes the selected code.
Before Embedding:
Embedded Code
After Embedding:
"Embedded Code"
Comment box
Embeds the selected text (code, comments) into the comment box.
Before Embedding:
// Embedded text
After Embedding:
/* •——————————————————•
| // Embedded text|
•——————————————————• */
-
The embedding is hidden in the Code Actions menu. Use the “Visible in the Code Actions (light bulb) menu” toolbar button on the Editor | All Languages | Selection Embeddings options page to show it.
-
This shortcut is disabled initially. For information on how to enable this shortcut, refer to the following topic section: Enable One-Key Selection Embedding Shortcuts