Skip to content

Modernize C# code snippets - System/Convert/* - #12949

Open
gewarren wants to merge 2 commits into
dotnet:mainfrom
gewarren:modernize-code-system-con
Open

Modernize C# code snippets - System/Convert/*#12949
gewarren wants to merge 2 commits into
dotnet:mainfrom
gewarren:modernize-code-system-con

Conversation

@gewarren

@gewarren gewarren commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Contributes to #12713.


Internal previews

Toggle expand/collapse
File Preview link
snippets/csharp/System/Convert/ChangeType/changetype_enum2.cs Preview published page
snippets/csharp/System/Convert/ChangeType/changetype_nullable_1.cs Preview published page
snippets/csharp/System/Convert/ChangeType/changetype_nullable.cs Preview published page
snippets/csharp/System/Convert/ChangeType/changetype00.cs Preview published page
snippets/csharp/System/Convert/ChangeType/changetype01.cs Preview published page
snippets/csharp/System/Convert/ChangeType/changetype03.cs Preview published page
snippets/csharp/System/Convert/ChangeType/convertchangetype.cs Preview published page
snippets/csharp/System/Convert/DBNull/dbnull1.cs Preview published page
snippets/csharp/System/Convert/FromBase64CharArray/class1.cs Preview published page
snippets/csharp/System/Convert/FromBase64CharArray/tb64ca.cs Preview published page
snippets/csharp/System/Convert/IsDBNull/Form1.cs Preview published page
snippets/csharp/System/Convert/Overview/converter.cs Preview published page
snippets/csharp/System/Convert/Overview/NonDecimal1.cs Preview published page
snippets/csharp/System/Convert/ToBase64String/tb64s.cs Preview published page
snippets/csharp/System/Convert/ToBoolean/objectifp.cs Preview published page
[snippets/csharp/System/Convert/ToBoolean/system.convert snippet.cs](https://github.com/dotnet/dotnet-api-docs/blob/5592cb22c247abd4a47d5b4dd4f0462cbaa576ea/snippets/csharp/System/Convert/ToBoolean/system.convert snippet.cs) Preview published page
snippets/csharp/System/Convert/ToBoolean/ToBoolean1.cs Preview published page
snippets/csharp/System/Convert/ToBoolean/toboolean2.cs Preview published page
snippets/csharp/System/Convert/ToByte/Conversion.cs Preview published page
snippets/csharp/System/Convert/ToChar/stringnonnum.cs Preview published page
snippets/csharp/System/Convert/ToChar/tochar1.cs Preview published page
snippets/csharp/System/Convert/ToDateTime/ToDateTime1.cs Preview published page
snippets/csharp/System/Convert/ToDateTime/ToDateTime2.cs Preview published page
snippets/csharp/System/Convert/ToDateTime/ToDateTime3.cs Preview published page
snippets/csharp/System/Convert/ToDateTime/todatetime4.cs Preview published page
snippets/csharp/System/Convert/ToDecimal/ToDecimal1.cs Preview published page
snippets/csharp/System/Convert/ToDecimal/todecimal11.cs Preview published page
snippets/csharp/System/Convert/ToDecimal/todecimal2.cs Preview published page
snippets/csharp/System/Convert/ToDecimal/todecimal3.cs Preview published page
snippets/csharp/System/Convert/ToDouble/example8.cs Preview published page

Note

This table shows the first 30 preview links (sorted alphabetically by file path) found in the OPS build report. For the full list, select OpenPublishing.Build Details within checks.

@gewarren
gewarren requested a review from a team as a code owner August 6, 2026 23:59
Copilot AI review requested due to automatic review settings August 6, 2026 23:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR continues the snippet-modernization effort for System.Convert (and a related Converter<,> example) by updating C# samples to more current syntax and formatting, aligning with issue #12713.

Changes:

  • Modernizes many snippets with target-typed new(), collection initializers, and expression-bodied members.
  • Replaces composite formatting with interpolated strings and updates control-flow formatting/spacing.
  • Cleans up some unnecessary type names/usings while keeping snippet outputs consistent.

Reviewed changes

Copilot reviewed 72 out of 74 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
snippets/csharp/System/ConverterTInput,TOutput/Overview/source.cs Uses collection initializer + spacing cleanups.
snippets/csharp/System/Convert/ToUInt64/touint64_3.cs Interpolated output + try/catch formatting.
snippets/csharp/System/Convert/ToUInt64/touint64_2.cs Target-typed new + interpolated output.
snippets/csharp/System/Convert/ToUInt32/touint32_3.cs Interpolated output + try/catch formatting.
snippets/csharp/System/Convert/ToUInt32/touint32_2.cs Target-typed new + interpolated output.
snippets/csharp/System/Convert/ToUInt16/touint16_4.cs Target-typed new + interpolated output.
snippets/csharp/System/Convert/ToUInt16/touint16_3.cs Modernizes IConvertible implementation + formatting.
snippets/csharp/System/Convert/ToUInt16/touint16_2.cs Interpolated output + try/catch formatting.
snippets/csharp/System/Convert/ToString/tostring7.cs Uses interpolated headers/rows and target-typed new.
snippets/csharp/System/Convert/ToString/tostring6.cs Simplifies formatting with interpolated strings.
snippets/csharp/System/Convert/ToString/tostring5.cs Converts properties/ctors to expression-bodied members.
snippets/csharp/System/Convert/ToString/tostring1.cs Updates many conversions to interpolated output and newer type keywords.
snippets/csharp/System/Convert/ToString/ToString.Byte1.cs Modern foreach typing + interpolated output.
snippets/csharp/System/Convert/ToString/tostring_string1.cs Uses string keywords + interpolation.
snippets/csharp/System/Convert/ToString/tostring_obj30.cs Modernizes IFormattable example + formatting.
snippets/csharp/System/Convert/ToString/ToString_Bool1.cs Minor formatting + type keyword modernization.
snippets/csharp/System/Convert/ToString/nonnumeric.cs Formatting cleanup + interpolation; removes unused using.
snippets/csharp/System/Convert/ToSingle/tosingle3.cs Interpolated output + structured try/catch blocks.
snippets/csharp/System/Convert/ToSingle/tosingle2.cs Expression-bodied members + type keyword updates.
snippets/csharp/System/Convert/ToSByte/tosbyte3.cs Interpolated output + structured try/catch blocks.
snippets/csharp/System/Convert/ToSByte/tosbyte2.cs Modernizes IConvertible implementation + formatting.
snippets/csharp/System/Convert/ToInt64/toint64_3.cs Target-typed new + interpolated output/formatting.
snippets/csharp/System/Convert/ToInt64/toint64_2.cs Interpolated output + structured try/catch blocks.
snippets/csharp/System/Convert/ToInt32/toint32_2.cs Target-typed new + interpolated output/formatting.
snippets/csharp/System/Convert/ToInt16/tosbyte.cs Formatting cleanup + target-typed new.
snippets/csharp/System/Convert/ToInt16/toint64.cs Formatting cleanup + target-typed new.
snippets/csharp/System/Convert/ToInt16/toint32.cs Formatting cleanup + target-typed new.
snippets/csharp/System/Convert/ToInt16/toint16.cs Formatting cleanup + target-typed new.
snippets/csharp/System/Convert/ToInt16/toint16_2.cs Interpolated output + structured try/catch blocks.
snippets/csharp/System/Convert/ToDouble/todouble1.cs Updates many outputs to interpolated strings.
snippets/csharp/System/Convert/ToDouble/todouble.cs Target-typed new + interpolated header/output formatting.
snippets/csharp/System/Convert/ToDouble/example8.cs Interpolated output + structured try/catch blocks.
snippets/csharp/System/Convert/ToDecimal/todecimal3.cs Interpolated output + structured try/catch blocks.
snippets/csharp/System/Convert/ToDecimal/todecimal2.cs Expression-bodied members + type keyword updates.
snippets/csharp/System/Convert/ToDecimal/todecimal11.cs Updates many outputs to interpolated strings.
snippets/csharp/System/Convert/ToDecimal/ToDecimal1.cs Formatting/indent fixes + interpolated output.
snippets/csharp/System/Convert/ToDateTime/todatetime4.cs Target-typed new + structured try/catch blocks.
snippets/csharp/System/Convert/ToDateTime/ToDateTime3.cs Interpolated table header + structured try/catch blocks.
snippets/csharp/System/Convert/ToDateTime/ToDateTime2.cs Type keyword updates + structured try/catch blocks.
snippets/csharp/System/Convert/ToDateTime/ToDateTime1.cs Type keyword updates + structured try/catch blocks.
snippets/csharp/System/Convert/ToChar/stringnonnum.cs Formatting cleanup + interpolation; removes unused using.
snippets/csharp/System/Convert/ToChar/strdatetime.cs Formatting cleanup + interpolation.
snippets/csharp/System/Convert/ToBoolean/ToBoolean1.cs Type keyword updates + structured try/catch blocks.
snippets/csharp/System/Convert/ToBase64String/tb64s.cs Formatting cleanup + interpolation.
snippets/csharp/System/Convert/Overview/NonDecimal1.cs Type keyword updates + interpolation.
snippets/csharp/System/Convert/Overview/converter.cs Whitespace/indent normalization + interpolation.
snippets/csharp/System/Convert/IsDBNull/Form1.cs Removes unused usings + formatting/indent cleanup.
snippets/csharp/System/Convert/FromBase64CharArray/tb64ca.cs Formatting cleanup + interpolation.
snippets/csharp/System/Convert/FromBase64CharArray/class1.cs Large formatting modernization + target-typed new.
snippets/csharp/System/Convert/DBNull/dbnull1.cs Formatting cleanup only.
snippets/csharp/System/Convert/ChangeType/convertchangetype.cs Type keyword updates + interpolation.
snippets/csharp/System/Convert/ChangeType/changetype03.cs Modernizes IConvertible sample + output formatting.
snippets/csharp/System/Convert/ChangeType/changetype01.cs Type keyword updates + interpolation.
snippets/csharp/System/Convert/ChangeType/changetype00.cs Structured try/catch + interpolation and formatting.
snippets/csharp/System/Convert/ChangeType/changetype_nullable.cs Type keyword updates + interpolation.
snippets/csharp/System/Convert/ChangeType/changetype_nullable_1.cs Type keyword updates + interpolation.
snippets/csharp/System/Convert/ChangeType/changetype_enum2.cs Type keyword updates + interpolation.

Comment thread snippets/csharp/System/Convert/ToInt64/toint64_3.cs Outdated
Comment on lines +23 to +26
if (value.Trim().Length > 4)
throw new ArgumentException("The string representation of a 160bit integer cannot have more than four characters.");
else if (!Regex.IsMatch(value, "([0-9,A-F]){1,4}", RegexOptions.IgnoreCase))
throw new ArgumentException("The hexadecimal representation of a 16-bit integer contains invalid characters.");
foreach (short number in numbers)
{
result = Convert.ToDouble(number);
Console.WriteLine($"Converted the UInt16 value {number} to {result}.");

public partial class Form1 : Form
{
private string connectionString = @"Data Source=RONPET59\SQLEXPRESS;Initial Catalog=SurveyDB;Integrated Security=True";
Comment on lines +40 to +42
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants