Modernize C# code snippets - System/Convert/* - #12949
Open
gewarren wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
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 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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Contributes to #12713.
Internal previews
Toggle expand/collapse
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.