From d3123dc0df1623039029ae4e165cb85d7abbb6b7 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Tue, 1 Sep 2026 19:29:35 +0200 Subject: [PATCH 1/7] SignedXml: document that instances are single-use (#13042) --- .../SignedXml.xml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/xml/System.Security.Cryptography.Xml/SignedXml.xml b/xml/System.Security.Cryptography.Xml/SignedXml.xml index 7d761bd390d..5cae703243c 100644 --- a/xml/System.Security.Cryptography.Xml/SignedXml.xml +++ b/xml/System.Security.Cryptography.Xml/SignedXml.xml @@ -188,6 +188,10 @@ If an application does not verify that external references seem appropriate for Given the risks of external references, throws an exception when an external reference is encountered. For more information about this issue, see [.NET applications encounter exception errors](https://support.microsoft.com/topic/after-you-apply-security-update-3141780-net-framework-applications-encounter-exception-errors-or-unexpected-failures-while-processing-files-that-contain-signedxml-922edd45-a91e-c755-bb30-2604acf37362). +## Instances are single-use + +A instance is not intended to be reused across multiple signing or verification operations. Starting in .NET 12, calling or more than once on the same instance, or calling , , or after a call to or has begun on the same instance, throws . This applies even if the earlier call itself threw an exception. Create a new instance for each signing or verification operation. + ]]> @@ -443,6 +447,7 @@ The following code example shows how to sign and verify a single element of an X ]]> + The instance has already been used to compute or verify a signature. @@ -510,6 +515,7 @@ The following code example shows how to sign and verify a single element of an X ]]> + The instance has already been used to compute or verify a signature. @@ -602,6 +608,7 @@ The following code example shows how to sign and verify a single element of an X -or The hash algorithm could not be created. + The instance has already been used to compute or verify a signature. @@ -667,6 +674,7 @@ The following code example shows how to sign and verify a single element of an X -or The hash algorithm could not be created. + The instance has already been used to compute or verify a signature. @@ -732,6 +740,7 @@ The following code example shows how to sign and verify a single element of an X -or- The cryptographic transform used to check the signature could not be created. + The instance has already been used to compute or verify a signature. @@ -794,6 +803,7 @@ The X.509 certificate is verified. The The parameter is . A signature description could not be created for the parameter. + The instance has already been used to compute or verify a signature. @@ -867,6 +877,7 @@ The X.509 certificate is verified. The + The instance has already been used to compute or verify a signature. @@ -944,6 +955,7 @@ The X.509 certificate is verified. The + The instance has already been used to compute or verify a signature. @@ -1014,6 +1026,7 @@ The X.509 certificate is verified. The + The instance has already been used to compute or verify a signature. @@ -1318,6 +1331,7 @@ The X.509 certificate is verified. The parameter does not contain a valid property. + The instance has already been used to compute or verify a signature. @@ -3046,3 +3060,4 @@ The X.509 certificate is verified. The + From 6ce6a271810accc29c97c4729abf18b13ec69bb3 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 1 Sep 2026 13:06:10 -0700 Subject: [PATCH 2/7] Modernize snippets (System.Diagnostics.Eventing.Reader) (#13040) --- .../EventLogInformation/Attributes/Project.csproj | 12 ++++++++++++ .../EventLogInformation/Attributes/attributes.cs | 9 ++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/Project.csproj diff --git a/snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/Project.csproj b/snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/Project.csproj new file mode 100644 index 00000000000..543f1390dc5 --- /dev/null +++ b/snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/Project.csproj @@ -0,0 +1,12 @@ + + + + Exe + net10.0 + + + + + + + diff --git a/snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/attributes.cs b/snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/attributes.cs index c28b52bf7ac..9d1907d9982 100644 --- a/snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/attributes.cs +++ b/snippets/csharp/System.Diagnostics.Eventing.Reader/EventLogInformation/Attributes/attributes.cs @@ -1,7 +1,10 @@ -// -using System.Diagnostics.Eventing.Reader; +using System.Diagnostics.Eventing.Reader; using System.IO; -EventLogInformation eventLogInformation; +using EventLogSession session = new(); +EventLogInformation eventLogInformation = + session.GetLogInformation("Application", PathType.LogName); + +// FileAttributes? fileAttributes = (FileAttributes?)eventLogInformation.Attributes; // From 71b9c6f3f4786e7e6d1758abebe60ea6ee15244f Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 1 Sep 2026 13:07:03 -0700 Subject: [PATCH 3/7] Simplify job name in ops-build-reporter.yml (#13038) --- .github/workflows/ops-build-reporter.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ops-build-reporter.yml b/.github/workflows/ops-build-reporter.yml index 95a2ff84cd9..2f00e75175b 100644 --- a/.github/workflows/ops-build-reporter.yml +++ b/.github/workflows/ops-build-reporter.yml @@ -9,7 +9,7 @@ permissions: jobs: ops_build_reporter_job: - name: Add preview links to PR body and annotate files with build warnings + name: Add preview links and annotate files runs-on: ubuntu-latest permissions: statuses: read From eee6890dc2c33ba429e9a5b14039dca17bec0d17 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 1 Sep 2026 13:34:32 -0700 Subject: [PATCH 4/7] Modernize remaining System.Diagnostics C# snippets (#13034) --- .../Overview/Project.csproj | 8 +- .../Overview/perfcountercatcreateexist.cs | 53 ++- .../Overview/Project.csproj | 7 +- .../Overview/averagetimer32.cs | 327 ++++++------- .../Overview/numberofitems32.cs | 118 ++--- .../Overview/numberofitems64.cs | 27 +- .../Overview/program.cs | 74 ++- .../Overview/rateofcountspersecond32.cs | 33 +- .../Overview/rateofcountspersecond64.cs | 33 +- .../Overview/rawfraction.cs | 34 +- .../PresentationTraceLevel/Overview/Person.cs | 76 ++-- .../Overview/SimpleBinding.csproj | 4 +- .../Process/ArgsEcho/Project.csproj | 7 + .../Process/ArgsEcho/processstartstatic3.cs | 13 + .../Process/BasePriority/Project.csproj | 6 +- .../Process/BasePriority/source.cs | 6 +- .../Process/Close/Project.csproj | 6 +- .../Process/Close/process_refresh.cs | 40 +- .../EnableRaisingEvents/Project.csproj | 6 +- .../EnableRaisingEvents/processexitedevent.cs | 7 +- .../Process/GetCurrentProcess/Project.csproj | 6 +- .../GetCurrentProcess/processstaticget.cs | 4 +- .../Process/Id/Project.csproj | 6 +- .../System.Diagnostics/Process/Id/program.cs | 10 +- .../Process/MachineName/Project.csproj | 6 +- .../process_getprocessesbyname2_2.cs | 4 +- .../Process/MainWindowTitle/Project.csproj | 6 +- .../process_mainwindowtitle.cs | 8 +- .../Process/OnExited/Project.csproj | 6 +- .../Process/OnExited/program.cs | 8 +- .../Process/Overview/Program.cs | 17 + .../Process/Overview/Project.csproj | 6 + .../Process/Overview/processstart.cs | 7 +- .../Process/Overview/processstartstatic.cs | 13 +- .../Process/Overview/processstartstatic2.cs | 21 +- .../Process/Overview/processstartstatic3.cs | 22 - .../Process/StandardError/Program.cs | 16 + .../Process/StandardError/Project.csproj | 3 - .../Process/StandardError/source.cs | 13 +- .../Process/StandardError/stderror-sync.cs | 28 +- .../Process/StandardInput/Project.csproj | 6 +- .../StandardInput/process_standardinput.cs | 7 +- .../Process/StandardOutput/Program.cs | 17 + .../Process/StandardOutput/Project.csproj | 3 - .../StandardOutput/process_standardoutput.cs | 10 +- .../Process/StandardOutput/stdoutput-async.cs | 8 +- .../Process/StandardOutput/stdoutput-sync.cs | 6 +- .../Process/Start/Project.csproj | 6 +- .../Process/Start/program.cs | 4 +- .../Process/SynchronizingObject/Program.cs | 21 + .../SynchronizingObject/Project.csproj | 7 + .../process_synchronizingobject.cs | 10 +- .../Process/SynchronizingObject/remarks.cs | 8 +- .../Process/Write500Lines/Project.csproj | 7 + .../write500lines.cs | 18 +- .../ProcessModule/BaseAddress/Project.csproj | 6 +- .../BaseAddress/processmodule_baseaddress.cs | 4 +- .../EntryPointAddress/Project.csproj | 6 +- .../processmodule_entrypoint.cs | 4 +- .../ProcessModule/FileName/Project.csproj | 6 +- .../FileName/processmodule_filename.cs | 4 +- .../FileVersionInfo/Project.csproj | 6 +- .../processmodule_fileversioninfo.cs | 4 +- .../ModuleMemorySize/Project.csproj | 6 +- .../processmodule_modulememorysize.cs | 4 +- .../ProcessModule/ModuleName/Project.csproj | 6 +- .../ModuleName/processmodule_modulename.cs | 4 +- .../ProcessModule/Overview/Project.csproj | 6 +- .../ProcessModule/Overview/processmodule.cs | 4 +- .../ProcessModule/ToString/Project.csproj | 6 +- .../ToString/processmodule_tostring.cs | 4 +- .../RedirectStandardOutput/Project.csproj | 6 +- .../RedirectStandardOutput/stdstr.cs | 2 +- .../ProcessStartInfo/Verb/Project.csproj | 8 +- .../ProcessStartInfo/Verb/source.cs | 47 +- .../IdealProcessor/Project.csproj | 6 +- .../ProcessThread/IdealProcessor/program.cs | 8 +- .../SourceFilter/.ctor/Project.csproj | 11 +- .../SourceFilter/.ctor/program.cs | 51 ++- .../StackFrame/GetMethod/Project.csproj | 10 +- .../StackFrame/GetMethod/stacktracesample3.cs | 14 +- .../StackFrame/Overview/Project.csproj | 6 +- .../StackFrame/Overview/program.cs | 47 +- .../StackFrame/Overview/source.cs | 428 +++++++++--------- .../StackTrace/GetFrames/Project.csproj | 6 +- .../StackTrace/GetFrames/stacktracesample2.cs | 22 +- .../StackTrace/Overview/Project.csproj | 6 +- .../StackTrace/Overview/stacktracesample1.cs | 28 +- .../Stopwatch/Overview/Program.cs | 22 + .../Stopwatch/Overview/Project.csproj | 6 +- .../Stopwatch/Overview/source.cs | 35 +- .../Stopwatch/Overview/source1.cs | 11 +- .../Switch/Overview/Program.cs | 14 + .../Switch/Overview/Project.csproj | 3 - .../Switch/Overview/remarks.cs | 6 +- .../Switch/Overview/source.cs | 4 +- .../SwitchAttribute/Overview/Project.csproj | 6 +- .../SwitchAttribute/Overview/program.cs | 11 +- .../TextWriterTraceListener/.ctor/Program.cs | 32 ++ .../.ctor/Project.csproj | 3 - .../TextWriterTraceListener/.ctor/source.cs | 16 +- .../TextWriterTraceListener/.ctor/source1.cs | 8 +- .../.ctor/twtlconstream.cs | 13 +- .../.ctor/twtlconstreamname.cs | 17 +- .../.ctor/twtlconstring.cs | 13 +- .../.ctor/twtlconstringname.cs | 17 +- .../.ctor/twtlconwritername.cs | 17 +- .../Overview/Project.csproj | 4 +- .../Overview/source.cs | 31 +- .../Write/Project.csproj | 4 +- .../TextWriterTraceListener/Write/source.cs | 29 +- .../Writer/Project.csproj | 4 +- .../TextWriterTraceListener/Writer/source.cs | 18 +- .../Trace/Assert/Project.csproj | 4 +- .../System.Diagnostics/Trace/Assert/source.cs | 7 +- .../Trace/Assert/source1.cs | 2 +- .../Trace/Assert/source2.cs | 2 +- .../Trace/Close/Project.csproj | 4 +- .../System.Diagnostics/Trace/Close/source.cs | 8 +- .../Trace/Fail/Project.csproj | 4 +- .../System.Diagnostics/Trace/Fail/source.cs | 13 +- .../System.Diagnostics/Trace/Fail/source1.cs | 13 +- .../Trace/Indent/Project.csproj | 4 +- .../System.Diagnostics/Trace/Indent/source.cs | 4 +- .../Trace/Listeners/Project.csproj | 4 +- .../Trace/Listeners/source.cs | 6 +- .../Trace/Overview/Project.csproj | 4 +- .../Trace/Overview/source.cs | 14 +- .../Trace/Write/Project.csproj | 6 + .../System.Diagnostics/Trace/Write/source.cs | 34 +- .../System.Diagnostics/Trace/Write/source1.cs | 34 +- .../System.Diagnostics/Trace/Write/source2.cs | 35 +- .../System.Diagnostics/Trace/Write/source3.cs | 34 +- .../Trace/WriteIf/Project.csproj | 6 + .../Trace/WriteIf/source.cs | 30 +- .../Trace/WriteIf/source1.cs | 30 +- .../Trace/WriteIf/source2.cs | 32 +- .../Trace/WriteIf/source3.cs | 30 +- .../Trace/WriteLine/Project.csproj | 6 + .../Trace/WriteLine/source.cs | 34 +- .../Trace/WriteLine/source1.cs | 34 +- .../Trace/WriteLine/source2.cs | 34 +- .../Trace/WriteLineIf/Project.csproj | 6 + .../Trace/WriteLineIf/source.cs | 30 +- .../Trace/WriteLineIf/source1.cs | 30 +- .../Trace/WriteLineIf/source2.cs | 30 +- .../TraceFilter/ShouldTrace/Project.csproj | 6 +- .../TraceFilter/ShouldTrace/source.cs | 14 +- .../GetSupportedAttributes/Project.csproj | 6 +- .../GetSupportedAttributes/program.cs | 10 +- .../Add/Project.csproj | 4 +- .../TraceListenerCollection/Add/source.cs | 7 +- .../TraceSwitch/Level/Project.csproj | 4 +- .../TraceSwitch/Level/source.cs | 17 +- .../TraceSwitch/Overview/Program.cs | 16 + .../TraceSwitch/Overview/Project.csproj | 3 - .../TraceSwitch/Overview/remarks.cs | 10 +- .../TraceSwitch/Overview/source.cs | 19 +- .../TraceSwitch/TraceInfo/Project.csproj | 4 +- .../TraceSwitch/TraceInfo/source.cs | 17 +- .../TraceSwitch/TraceWarning/Project.csproj | 4 +- .../TraceSwitch/TraceWarning/source.cs | 17 +- .../Overview/Project.csproj | 6 +- .../Overview/program.cs | 12 +- xml/System.Diagnostics/Process.xml | 4 +- xml/System.Diagnostics/ProcessStartInfo.xml | 6 +- 166 files changed, 1640 insertions(+), 1477 deletions(-) create mode 100644 snippets/csharp/System.Diagnostics/Process/ArgsEcho/Project.csproj create mode 100644 snippets/csharp/System.Diagnostics/Process/ArgsEcho/processstartstatic3.cs create mode 100644 snippets/csharp/System.Diagnostics/Process/Overview/Program.cs create mode 100644 snippets/csharp/System.Diagnostics/Process/Overview/Project.csproj delete mode 100644 snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic3.cs create mode 100644 snippets/csharp/System.Diagnostics/Process/StandardError/Program.cs create mode 100644 snippets/csharp/System.Diagnostics/Process/StandardOutput/Program.cs create mode 100644 snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Program.cs create mode 100644 snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Project.csproj create mode 100644 snippets/csharp/System.Diagnostics/Process/Write500Lines/Project.csproj rename snippets/csharp/System.Diagnostics/Process/{StandardOutput => Write500Lines}/write500lines.cs (53%) create mode 100644 snippets/csharp/System.Diagnostics/Stopwatch/Overview/Program.cs create mode 100644 snippets/csharp/System.Diagnostics/Switch/Overview/Program.cs create mode 100644 snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Program.cs create mode 100644 snippets/csharp/System.Diagnostics/Trace/Write/Project.csproj create mode 100644 snippets/csharp/System.Diagnostics/Trace/WriteIf/Project.csproj create mode 100644 snippets/csharp/System.Diagnostics/Trace/WriteLine/Project.csproj create mode 100644 snippets/csharp/System.Diagnostics/Trace/WriteLineIf/Project.csproj create mode 100644 snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Program.cs diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/Project.csproj index e17ba622dbc..a923b5feafb 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/Project.csproj @@ -1,13 +1,9 @@ - - Library - net10.0-windows - true + Exe + net10.0 - - diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/perfcountercatcreateexist.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/perfcountercatcreateexist.cs index 4f0ed62852e..ed972357b59 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/perfcountercatcreateexist.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterCategory/Overview/perfcountercatcreateexist.cs @@ -1,7 +1,6 @@ // using System; using System.Diagnostics; -using Microsoft.VisualBasic; class PerfCounterCatCreateExistMod { @@ -44,7 +43,7 @@ public static void Main(string[] args) // instanceName = args[2]; // - machineName = args[3]=="."? "": args[3]; + machineName = args[3] == "." ? "" : args[3]; // // // @@ -54,13 +53,13 @@ public static void Main(string[] args) // // } - catch(Exception ex) + catch (Exception) { // Ignore the exception from non-supplied arguments. } // Verify that the category name is not blank. - if (categoryName.Length==0) + if (categoryName.Length == 0) { Console.WriteLine("Category name cannot be blank."); return; @@ -69,8 +68,8 @@ public static void Main(string[] args) // // Check whether the specified category exists. // - if (machineName.Length==0) - // + if (machineName.Length == 0) + // { objectExists = PerformanceCounterCategory.Exists(categoryName); @@ -84,22 +83,22 @@ public static void Main(string[] args) { objectExists = PerformanceCounterCategory.Exists(categoryName, machineName); } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine("Error checking for existence of " + - "category \"{0}\" on computer \"{1}\":"+"\n" +ex.Message, categoryName, machineName); + "category \"{0}\" on computer \"{1}\":" + "\n" + ex.Message, categoryName, machineName); return; } } // // Tell the user whether the specified category exists. - Console.WriteLine("Category \"{0}\" "+ (objectExists? "exists on ": "does not exist on ")+ - (machineName.Length>0? "computer \"{1}\".": "this computer."), categoryName, machineName); + Console.WriteLine("Category \"{0}\" " + (objectExists ? "exists on " : "does not exist on ") + + (machineName.Length > 0 ? "computer \"{1}\"." : "this computer."), categoryName, machineName); // // If no counter name is given, the program cannot continue. - if (counterName.Length==0) + if (counterName.Length == 0) { return; } @@ -107,10 +106,10 @@ public static void Main(string[] args) // A category can only be created on the local computer. // if (!objectExists) - // + // { - if (machineName.Length>0) - // + if (machineName.Length > 0) + // { return; // @@ -126,7 +125,7 @@ public static void Main(string[] args) { // // Check whether the specified counter exists. - if (machineName.Length==0) + if (machineName.Length == 0) { objectExists = PerformanceCounterCategory.CounterExists(counterName, categoryName); } @@ -136,18 +135,18 @@ public static void Main(string[] args) } // Tell the user whether the counter exists. - Console.WriteLine("Counter \"{0}\" "+(objectExists? "exists": "does not exist")+ - " in category \"{1}\" on "+(machineName.Length>0? "computer \"{2}\".": "this computer."), + Console.WriteLine("Counter \"{0}\" " + (objectExists ? "exists" : "does not exist") + + " in category \"{1}\" on " + (machineName.Length > 0 ? "computer \"{2}\"." : "this computer."), counterName, categoryName, machineName); // // If the counter does not exist, consider creating it. if (!objectExists) - // If this is a remote computer, - // exit because the category cannot be created. + // If this is a remote computer, + // exit because the category cannot be created. { - if (machineName.Length>0) + if (machineName.Length > 0) { return; } @@ -159,8 +158,8 @@ public static void Main(string[] args) string userReply = Console.ReadLine(); // If yes, delete the category so it can be recreated later. - if (userReply.Trim().ToUpper()=="Y") - // + if (userReply.Trim().ToUpper() == "Y") + // { PerformanceCounterCategory.Delete(categoryName); // @@ -176,16 +175,16 @@ public static void Main(string[] args) // Create the category if it was deleted or it never existed. if (createCategory) - // + // { pcc = PerformanceCounterCategory.Create(categoryName, categoryHelp, counterName, counterHelp); Console.WriteLine("Category \"{0}\" with counter \"{1}\" created.", pcc.CategoryName, counterName); // } - else if(instanceName.Length>0) + else if (instanceName.Length > 0) { - if (machineName.Length==0) + if (machineName.Length == 0) { objectExists = PerformanceCounterCategory.InstanceExists(instanceName, categoryName); } @@ -195,8 +194,8 @@ public static void Main(string[] args) } // Tell the user whether the instance exists. - Console.WriteLine("Instance \"{0}\" "+(objectExists? "exists": "does not exist")+ - " in category \"{1}\" on " + (machineName.Length>0? "computer \"{2}\".": "this computer."), + Console.WriteLine("Instance \"{0}\" " + (objectExists ? "exists" : "does not exist") + + " in category \"{1}\" on " + (machineName.Length > 0 ? "computer \"{2}\"." : "this computer."), instanceName, categoryName, machineName); // } diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/Project.csproj index 2da0e2c4887..a06a460e5da 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/Project.csproj @@ -1,12 +1,9 @@ - - Library + Exe net10.0 - - - + diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/averagetimer32.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/averagetimer32.cs index a729d165f2b..38a8a37267f 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/averagetimer32.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/averagetimer32.cs @@ -16,115 +16,120 @@ public class App { - private static PerformanceCounter PC; - private static PerformanceCounter BPC; - - public static void Main() - { - ArrayList samplesList = new ArrayList(); - - SetupCategory(); - CreateCounters(); - CollectSamples(samplesList); - CalculateResults(samplesList); - } - - - - - private static bool SetupCategory() - { - - if ( !PerformanceCounterCategory.Exists("AverageTimer32SampleCategory") ) - { - - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); - - // Add the counter. - CounterCreationData averageTimer32 = new CounterCreationData(); - averageTimer32.CounterType = PerformanceCounterType.AverageTimer32; - averageTimer32.CounterName = "AverageTimer32Sample"; - CCDC.Add(averageTimer32); - - // Add the base counter. - CounterCreationData averageTimer32Base = new CounterCreationData(); - averageTimer32Base.CounterType = PerformanceCounterType.AverageBase; - averageTimer32Base.CounterName = "AverageTimer32SampleBase"; - CCDC.Add(averageTimer32Base); - - // Create the category. - PerformanceCounterCategory.Create("AverageTimer32SampleCategory", - "Demonstrates usage of the AverageTimer32 performance counter type", - CCDC); - - return(true); - } - else - { - Console.WriteLine("Category exists - " + "AverageTimer32SampleCategory"); - return(false); - } - } - - private static void CreateCounters() - { - // Create the counters. - PC = new PerformanceCounter("AverageTimer32SampleCategory", - "AverageTimer32Sample", - false); - - BPC = new PerformanceCounter("AverageTimer32SampleCategory", - "AverageTimer32SampleBase", - false); - - PC.RawValue = 0; - BPC.RawValue = 0; - } - - - private static void CollectSamples(ArrayList samplesList) - { - - long perfTime = 0; - Random r = new Random( DateTime.Now.Millisecond ); - - // Loop for the samples. - for (int i = 0; i < 10; i++) { - - QueryPerformanceCounter(out perfTime); - PC.RawValue = perfTime; - - BPC.IncrementBy(10); - - System.Threading.Thread.Sleep(1000); - Console.WriteLine("Next value = " + PC.NextValue().ToString()); - samplesList.Add(PC.NextSample()); - } + private static PerformanceCounter PC; + private static PerformanceCounter BPC; + + public static void Run() + { + ArrayList samplesList = []; + SetupCategory(); + CreateCounters(); + CollectSamples(samplesList); + CalculateResults(samplesList); } - private static void CalculateResults(ArrayList samplesList) - { - for(int i = 0; i < (samplesList.Count - 1); i++) - { - // Output the sample. - OutputSample( (CounterSample)samplesList[i] ); - OutputSample( (CounterSample)samplesList[i+1] ); - - // Use .NET to calculate the counter value. - Console.WriteLine(".NET computed counter value = " + - CounterSample.Calculate((CounterSample)samplesList[i], - (CounterSample)samplesList[i+1]) ); - - // Calculate the counter value manually. - Console.WriteLine("My computed counter value = " + - MyComputeCounterValue((CounterSample)samplesList[i], - (CounterSample)samplesList[i+1]) ); - - } - } - - + + + + private static bool SetupCategory() + { + + if (!PerformanceCounterCategory.Exists("AverageTimer32SampleCategory")) + { + + CounterCreationDataCollection CCDC = []; + + // Add the counter. + CounterCreationData averageTimer32 = new() + { + CounterType = PerformanceCounterType.AverageTimer32, + CounterName = "AverageTimer32Sample" + }; + CCDC.Add(averageTimer32); + + // Add the base counter. + CounterCreationData averageTimer32Base = new() + { + CounterType = PerformanceCounterType.AverageBase, + CounterName = "AverageTimer32SampleBase" + }; + CCDC.Add(averageTimer32Base); + + // Create the category. + PerformanceCounterCategory.Create("AverageTimer32SampleCategory", + "Demonstrates usage of the AverageTimer32 performance counter type", + CCDC); + + return (true); + } + else + { + Console.WriteLine("Category exists - " + "AverageTimer32SampleCategory"); + return (false); + } + } + + private static void CreateCounters() + { + // Create the counters. + PC = new PerformanceCounter("AverageTimer32SampleCategory", + "AverageTimer32Sample", + false); + + BPC = new PerformanceCounter("AverageTimer32SampleCategory", + "AverageTimer32SampleBase", + false); + + PC.RawValue = 0; + BPC.RawValue = 0; + } + + + private static void CollectSamples(ArrayList samplesList) + { + + long perfTime = 0; + Random r = new(DateTime.Now.Millisecond); + + // Loop for the samples. + for (int i = 0; i < 10; i++) + { + + QueryPerformanceCounter(out perfTime); + PC.RawValue = perfTime; + + BPC.IncrementBy(10); + + System.Threading.Thread.Sleep(1000); + Console.WriteLine($"Next value = {PC.NextValue()}"); + samplesList.Add(PC.NextSample()); + } + + } + + private static void CalculateResults(ArrayList samplesList) + { + for (int i = 0; i < (samplesList.Count - 1); i++) + { + // Output the sample. + OutputSample((CounterSample)samplesList[i]); + OutputSample((CounterSample)samplesList[i + 1]); + + // Use .NET to calculate the counter value. + Console.WriteLine(".NET computed counter value = " + + CounterSample.Calculate((CounterSample)samplesList[i], + (CounterSample)samplesList[i + 1])); + + // Calculate the counter value manually. + Console.WriteLine("My computed counter value = " + + MyComputeCounterValue((CounterSample)samplesList[i], + (CounterSample)samplesList[i + 1])); + + } + } + + //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//+++++++ // PERF_AVERAGE_TIMER @@ -149,39 +154,39 @@ private static void CalculateResults(ArrayList samplesList) // // Example - PhysicalDisk\ Avg. Disk sec/Transfer //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//+++++++ - private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1) - { - Int64 n1 = s1.RawValue; - Int64 n0 = s0.RawValue; - ulong f = (ulong)s1.SystemFrequency; - Int64 d1 = s1.BaseValue; - Int64 d0 = s0.BaseValue; - - double numerator = (double)(n1 - n0); - double denominator = (double)(d1 - d0); - Single counterValue = (Single)(( numerator / f ) / denominator); - return(counterValue); - } - - // Output information about the counter sample. - private static void OutputSample(CounterSample s) - { - Console.WriteLine("+++++++++++"); - Console.WriteLine("Sample values - \r\n"); - Console.WriteLine(" BaseValue = " + s.BaseValue); - Console.WriteLine(" CounterFrequency = " + s.CounterFrequency); - Console.WriteLine(" CounterTimeStamp = " + s.CounterTimeStamp); - Console.WriteLine(" CounterType = " + s.CounterType); - Console.WriteLine(" RawValue = " + s.RawValue); - Console.WriteLine(" SystemFrequency = " + s.SystemFrequency); - Console.WriteLine(" TimeStamp = " + s.TimeStamp); - Console.WriteLine(" TimeStamp100nSec = " + s.TimeStamp100nSec); - Console.WriteLine("++++++++++++++++++++++"); - } - - - [DllImport("Kernel32.dll")] - public static extern bool QueryPerformanceCounter(out long value); + private static float MyComputeCounterValue(CounterSample s0, CounterSample s1) + { + long n1 = s1.RawValue; + long n0 = s0.RawValue; + ulong f = (ulong)s1.SystemFrequency; + long d1 = s1.BaseValue; + long d0 = s0.BaseValue; + + double numerator = (double)(n1 - n0); + double denominator = (double)(d1 - d0); + float counterValue = (float)((numerator / f) / denominator); + return (counterValue); + } + + // Output information about the counter sample. + private static void OutputSample(CounterSample s) + { + Console.WriteLine("+++++++++++"); + Console.WriteLine("Sample values - \r\n"); + Console.WriteLine(" BaseValue = " + s.BaseValue); + Console.WriteLine(" CounterFrequency = " + s.CounterFrequency); + Console.WriteLine(" CounterTimeStamp = " + s.CounterTimeStamp); + Console.WriteLine(" CounterType = " + s.CounterType); + Console.WriteLine(" RawValue = " + s.RawValue); + Console.WriteLine(" SystemFrequency = " + s.SystemFrequency); + Console.WriteLine(" TimeStamp = " + s.TimeStamp); + Console.WriteLine(" TimeStamp100nSec = " + s.TimeStamp100nSec); + Console.WriteLine("++++++++++++++++++++++"); + } + + + [DllImport("Kernel32.dll")] + public static extern bool QueryPerformanceCounter(out long value); } @@ -201,13 +206,13 @@ public class App2 private static PerformanceCounter PC; private static PerformanceCounter BPC; - private const String categoryName = "AverageTimer32SampleCategory"; - private const String counterName = "AverageTimer32Sample"; - private const String baseCounterName = "AverageTimer32SampleBase"; + private const string categoryName = "AverageTimer32SampleCategory"; + private const string counterName = "AverageTimer32Sample"; + private const string baseCounterName = "AverageTimer32SampleBase"; - public static void Main() + public static void Run() { - ArrayList samplesList = new ArrayList(); + ArrayList samplesList = []; // If the category does not exist, create the category and exit. // Performance counters should not be created and immediately used. @@ -226,18 +231,22 @@ private static bool SetupCategory() if (!PerformanceCounterCategory.Exists(categoryName)) { - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); + CounterCreationDataCollection CCDC = []; // Add the counter. - CounterCreationData averageTimer32 = new CounterCreationData(); - averageTimer32.CounterType = PerformanceCounterType.AverageTimer32; - averageTimer32.CounterName = counterName; + CounterCreationData averageTimer32 = new() + { + CounterType = PerformanceCounterType.AverageTimer32, + CounterName = counterName + }; CCDC.Add(averageTimer32); // Add the base counter. - CounterCreationData averageTimer32Base = new CounterCreationData(); - averageTimer32Base.CounterType = PerformanceCounterType.AverageBase; - averageTimer32Base.CounterName = baseCounterName; + CounterCreationData averageTimer32Base = new() + { + CounterType = PerformanceCounterType.AverageBase, + CounterName = baseCounterName + }; CCDC.Add(averageTimer32Base); // Create the category. @@ -274,7 +283,7 @@ private static void CreateCounters() private static void CollectSamples(ArrayList samplesList) { - Random r = new Random(DateTime.Now.Millisecond); + Random r = new(DateTime.Now.Millisecond); // Loop for the samples. for (int i = 0; i < 10; i++) @@ -286,7 +295,7 @@ private static void CollectSamples(ArrayList samplesList) System.Threading.Thread.Sleep(1000); - Console.WriteLine("Next value = " + PC.NextValue().ToString()); + Console.WriteLine($"Next value = {PC.NextValue()}"); samplesList.Add(PC.NextSample()); } } @@ -334,17 +343,17 @@ private static void CalculateResults(ArrayList samplesList) // // Example - PhysicalDisk\ Avg. Disk sec/Transfer //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//+++++++ - private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1) + private static float MyComputeCounterValue(CounterSample s0, CounterSample s1) { - Int64 n1 = s1.RawValue; - Int64 n0 = s0.RawValue; + long n1 = s1.RawValue; + long n0 = s0.RawValue; ulong f = (ulong)s1.SystemFrequency; - Int64 d1 = s1.BaseValue; - Int64 d0 = s0.BaseValue; + long d1 = s1.BaseValue; + long d0 = s0.BaseValue; double numerator = (double)(n1 - n0); double denominator = (double)(d1 - d0); - Single counterValue = (Single)((numerator / f) / denominator); + float counterValue = (float)((numerator / f) / denominator); return (counterValue); } diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems32.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems32.cs index a403df5add5..88affdf4dc5 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems32.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems32.cs @@ -6,11 +6,11 @@ public class NumberOfItems32 { - private static PerformanceCounter PC; + private static PerformanceCounter PC; - public static void Main() - { - ArrayList samplesList = []; + public static void Run() + { + ArrayList samplesList = []; // If the category does not exist, create the category and exit. // Performance counters should not be created and immediately used. @@ -21,19 +21,19 @@ public static void Main() return; CreateCounters(); - CollectSamples(samplesList); - CalculateResults(samplesList); - } + CollectSamples(samplesList); + CalculateResults(samplesList); + } private static bool SetupCategory() { - if ( !PerformanceCounterCategory.Exists("NumberOfItems32SampleCategory") ) + if (!PerformanceCounterCategory.Exists("NumberOfItems32SampleCategory")) { - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); + CounterCreationDataCollection CCDC = []; // Add the counter. - CounterCreationData NOI32 = new CounterCreationData + CounterCreationData NOI32 = new() { CounterType = PerformanceCounterType.NumberOfItems32, CounterName = "NumberOfItems32Sample" @@ -45,12 +45,12 @@ private static bool SetupCategory() "Demonstrates usage of the NumberOfItems32 performance counter type.", PerformanceCounterCategoryType.SingleInstance, CCDC); - return(true); + return (true); } else { Console.WriteLine("Category exists - NumberOfItems32SampleCategory"); - return(false); + return (false); } } @@ -66,76 +66,76 @@ private static void CreateCounters() }; } - private static void CollectSamples(ArrayList samplesList) - { + private static void CollectSamples(ArrayList samplesList) + { - Random r = new( DateTime.Now.Millisecond ); + Random r = new(DateTime.Now.Millisecond); - // Loop for the samples. - for (int j = 0; j < 100; j++) - { + // Loop for the samples. + for (int j = 0; j < 100; j++) + { - int value = r.Next(1, 10); - Console.Write(j + " = " + value); + int value = r.Next(1, 10); + Console.Write(j + " = " + value); - PC.IncrementBy(value); + PC.IncrementBy(value); - if ((j % 10) == 9) - { - OutputSample(PC.NextSample()); - samplesList.Add( PC.NextSample() ); - } - else + if ((j % 10) == 9) + { + OutputSample(PC.NextSample()); + samplesList.Add(PC.NextSample()); + } + else { Console.WriteLine(); } System.Threading.Thread.Sleep(50); - } - } + } + } private static void CalculateResults(ArrayList samplesList) { - for(int i = 0; i < (samplesList.Count - 1); i++) + for (int i = 0; i < (samplesList.Count - 1); i++) { // Output the sample. - OutputSample( (CounterSample)samplesList[i] ); - OutputSample( (CounterSample)samplesList[i+1] ); + OutputSample((CounterSample)samplesList[i]); + OutputSample((CounterSample)samplesList[i + 1]); - // Use .NET to calculate the counter value. + // Use .NET to calculate the counter value. Console.WriteLine(".NET computed counter value = " + CounterSampleCalculator.ComputeCounterValue((CounterSample)samplesList[i], - (CounterSample)samplesList[i+1]) ); + (CounterSample)samplesList[i + 1])); - // Calculate the counter value manually. + // Calculate the counter value manually. Console.WriteLine("My computed counter value = " + MyComputeCounterValue((CounterSample)samplesList[i], - (CounterSample)samplesList[i+1]) ); + (CounterSample)samplesList[i + 1])); } } - //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ - //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ - private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1) - { - Single counterValue = s1.RawValue; - return(counterValue); - } - - // Output information about the counter sample. - private static void OutputSample(CounterSample s) - { - Console.WriteLine("\r\n+++++++++++"); - Console.WriteLine("Sample values - \r\n"); - Console.WriteLine(" BaseValue = " + s.BaseValue); - Console.WriteLine(" CounterFrequency = " + s.CounterFrequency); - Console.WriteLine(" CounterTimeStamp = " + s.CounterTimeStamp); - Console.WriteLine(" CounterType = " + s.CounterType); - Console.WriteLine(" RawValue = " + s.RawValue); - Console.WriteLine(" SystemFrequency = " + s.SystemFrequency); - Console.WriteLine(" TimeStamp = " + s.TimeStamp); - Console.WriteLine(" TimeStamp100nSec = " + s.TimeStamp100nSec); - Console.WriteLine("++++++++++++++++++++++"); - } + //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ + //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ + private static float MyComputeCounterValue(CounterSample s0, CounterSample s1) + { + float counterValue = s1.RawValue; + return (counterValue); + } + + // Output information about the counter sample. + private static void OutputSample(CounterSample s) + { + Console.WriteLine("\r\n+++++++++++"); + Console.WriteLine("Sample values - \r\n"); + Console.WriteLine(" BaseValue = " + s.BaseValue); + Console.WriteLine(" CounterFrequency = " + s.CounterFrequency); + Console.WriteLine(" CounterTimeStamp = " + s.CounterTimeStamp); + Console.WriteLine(" CounterType = " + s.CounterType); + Console.WriteLine(" RawValue = " + s.RawValue); + Console.WriteLine(" SystemFrequency = " + s.SystemFrequency); + Console.WriteLine(" TimeStamp = " + s.TimeStamp); + Console.WriteLine(" TimeStamp100nSec = " + s.TimeStamp100nSec); + Console.WriteLine("++++++++++++++++++++++"); + } } // diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems64.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems64.cs index ca73e6be7ff..b2914b77203 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems64.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/numberofitems64.cs @@ -9,9 +9,9 @@ public class NumberOfItems64_1 { private static PerformanceCounter PC; - public static void Main() + public static void Run() { - ArrayList samplesList = new ArrayList(); + ArrayList samplesList = []; // If the category does not exist, create the category and exit. // Perfomance counters should not be created and immediately used. @@ -29,12 +29,14 @@ private static bool SetupCategory() { if (!PerformanceCounterCategory.Exists("NumberOfItems64SampleCategory")) { - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); + CounterCreationDataCollection CCDC = []; // Add the counter. - CounterCreationData NOI64 = new CounterCreationData(); - NOI64.CounterType = PerformanceCounterType.NumberOfItems64; - NOI64.CounterName = "NumberOfItems64Sample"; + CounterCreationData NOI64 = new() + { + CounterType = PerformanceCounterType.NumberOfItems64, + CounterName = "NumberOfItems64Sample" + }; CCDC.Add(NOI64); // Create the category. @@ -55,14 +57,15 @@ private static void CreateCounters() // Create the counters. PC = new PerformanceCounter("NumberOfItems64SampleCategory", "NumberOfItems64Sample", - false); - - PC.RawValue = 0; + false) + { + RawValue = 0 + }; } private static void CollectSamples(ArrayList samplesList) { - Random r = new Random(DateTime.Now.Millisecond); + Random r = new(DateTime.Now.Millisecond); // Loop for the samples. for (int j = 0; j < 100; j++) @@ -109,9 +112,9 @@ private static void CalculateResults(ArrayList samplesList) //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ - private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1) + private static float MyComputeCounterValue(CounterSample s0, CounterSample s1) { - Single counterValue = s1.RawValue; + float counterValue = s1.RawValue; return (counterValue); } diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/program.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/program.cs index 6cdc59d744b..24ea0b3a795 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/program.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/program.cs @@ -6,17 +6,17 @@ // Provides a SampleFraction counter to measure the percentage of the user processor // time for this process to total processor time for the process. -public class App +public class SampleFractionApp { private static PerformanceCounter perfCounter; private static PerformanceCounter basePerfCounter; private static Process thisProcess = Process.GetCurrentProcess(); - public static void Main() + public static void Run() { - ArrayList samplesList = new ArrayList(); + ArrayList samplesList = []; // If the category does not exist, create the category and exit. // Performance counters should not be created and immediately used. @@ -35,18 +35,22 @@ private static bool SetupCategory() if (!PerformanceCounterCategory.Exists("SampleFractionCategory")) { - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); + CounterCreationDataCollection CCDC = []; // Add the counter. - CounterCreationData sampleFraction = new CounterCreationData(); - sampleFraction.CounterType = PerformanceCounterType.SampleFraction; - sampleFraction.CounterName = "SampleFractionSample"; + CounterCreationData sampleFraction = new() + { + CounterType = PerformanceCounterType.SampleFraction, + CounterName = "SampleFractionSample" + }; CCDC.Add(sampleFraction); // Add the base counter. - CounterCreationData sampleFractionBase = new CounterCreationData(); - sampleFractionBase.CounterType = PerformanceCounterType.SampleBase; - sampleFractionBase.CounterName = "SampleFractionSampleBase"; + CounterCreationData sampleFractionBase = new() + { + CounterType = PerformanceCounterType.SampleBase, + CounterName = "SampleFractionSampleBase" + }; CCDC.Add(sampleFractionBase); // Create the category. @@ -128,11 +132,11 @@ private static void CalculateResults(ArrayList samplesList) // average ratio of user proccessor time to total processor time during the last // two sample intervals. //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ - private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1) + private static float MyComputeCounterValue(CounterSample s0, CounterSample s1) { - Single numerator = (Single)s1.RawValue - (Single)s0.RawValue; - Single denomenator = (Single)s1.BaseValue - (Single)s0.BaseValue; - Single counterValue = 100 * (numerator / denomenator); + float numerator = (float)s1.RawValue - (float)s0.RawValue; + float denomenator = (float)s1.BaseValue - (float)s0.BaseValue; + float counterValue = 100 * (numerator / denomenator); return (counterValue); } @@ -152,4 +156,44 @@ private static void OutputSample(CounterSample s) Console.WriteLine("++++++++++++++++++++++"); } } -// \ No newline at end of file +// + +internal static class Program +{ + public static void Main(string[] args) + { + switch (args.Length > 0 ? args[0] : null) + { + case "average-timer": +#if BELOW_WHIDBEY_BUILD + App.Run(); +#else + App2.Run(); +#endif + break; + case "number-of-items-32": + NumberOfItems32.Run(); + break; + case "number-of-items-64": + NumberOfItems64_1.Run(); + break; + case "sample-fraction": + SampleFractionApp.Run(); + break; + case "rate-32": + App3.Run(); + break; + case "rate-64": + App4.Run(); + break; + case "raw-fraction": + App5.Run(); + break; + default: + Console.WriteLine( + "Specify: average-timer, number-of-items-32, " + + "number-of-items-64, sample-fraction, rate-32, rate-64, or raw-fraction."); + break; + } + } +} diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond32.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond32.cs index 0ccd952350c..f5635c3513d 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond32.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond32.cs @@ -9,9 +9,9 @@ public class App3 { private static PerformanceCounter PC; - public static void Main() + public static void Run() { - ArrayList samplesList = new ArrayList(); + ArrayList samplesList = []; // If the category does not exist, create the category and exit. // Perfomance counters should not be created and immediately used. @@ -30,12 +30,14 @@ private static bool SetupCategory() if (!PerformanceCounterCategory.Exists("RateOfCountsPerSecond32SampleCategory")) { - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); + CounterCreationDataCollection CCDC = []; // Add the counter. - CounterCreationData rateOfCounts32 = new CounterCreationData(); - rateOfCounts32.CounterType = PerformanceCounterType.RateOfCountsPerSecond32; - rateOfCounts32.CounterName = "RateOfCountsPerSecond32Sample"; + CounterCreationData rateOfCounts32 = new() + { + CounterType = PerformanceCounterType.RateOfCountsPerSecond32, + CounterName = "RateOfCountsPerSecond32Sample" + }; CCDC.Add(rateOfCounts32); // Create the category. @@ -56,15 +58,16 @@ private static void CreateCounters() // Create the counter. PC = new PerformanceCounter("RateOfCountsPerSecond32SampleCategory", "RateOfCountsPerSecond32Sample", - false); - - PC.RawValue = 0; + false) + { + RawValue = 0 + }; } private static void CollectSamples(ArrayList samplesList) { - Random r = new Random(DateTime.Now.Millisecond); + Random r = new(DateTime.Now.Millisecond); // Initialize the performance counter. PC.NextSample(); @@ -79,7 +82,7 @@ private static void CollectSamples(ArrayList samplesList) if ((j % 10) == 9) { - Console.WriteLine("; NextValue() = " + PC.NextValue().ToString()); + Console.WriteLine($"; NextValue() = {PC.NextValue()}"); OutputSample(PC.NextSample()); samplesList.Add(PC.NextSample()); } @@ -131,11 +134,11 @@ private static void CalculateResults(ArrayList samplesList) // // Example - System\ File Read Operations/sec //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ - private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1) + private static float MyComputeCounterValue(CounterSample s0, CounterSample s1) { - Single numerator = (Single)(s1.RawValue - s0.RawValue); - Single denomenator = (Single)(s1.TimeStamp - s0.TimeStamp) / (Single)s1.SystemFrequency; - Single counterValue = numerator / denomenator; + float numerator = (float)(s1.RawValue - s0.RawValue); + float denomenator = (float)(s1.TimeStamp - s0.TimeStamp) / (float)s1.SystemFrequency; + float counterValue = numerator / denomenator; return (counterValue); } diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond64.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond64.cs index 8e3c492d862..786f5d30d1a 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond64.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rateofcountspersecond64.cs @@ -9,9 +9,9 @@ public class App4 { private static PerformanceCounter PC; - public static void Main() + public static void Run() { - ArrayList samplesList = new ArrayList(); + ArrayList samplesList = []; // If the category does not exist, create the category and exit. // Perfomance counters should not be created and immediately used. @@ -31,12 +31,14 @@ private static bool SetupCategory() if (!PerformanceCounterCategory.Exists("RateOfCountsPerSecond64SampleCategory")) { - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); + CounterCreationDataCollection CCDC = []; // Add the counter. - CounterCreationData rateOfCounts64 = new CounterCreationData(); - rateOfCounts64.CounterType = PerformanceCounterType.RateOfCountsPerSecond64; - rateOfCounts64.CounterName = "RateOfCountsPerSecond64Sample"; + CounterCreationData rateOfCounts64 = new() + { + CounterType = PerformanceCounterType.RateOfCountsPerSecond64, + CounterName = "RateOfCountsPerSecond64Sample" + }; CCDC.Add(rateOfCounts64); // Create the category. @@ -57,15 +59,16 @@ private static void CreateCounters() // Create the counter. PC = new PerformanceCounter("RateOfCountsPerSecond64SampleCategory", "RateOfCountsPerSecond64Sample", - false); - - PC.RawValue = 0; + false) + { + RawValue = 0 + }; } private static void CollectSamples(ArrayList samplesList) { - Random r = new Random(DateTime.Now.Millisecond); + Random r = new(DateTime.Now.Millisecond); // Initialize the performance counter. PC.NextSample(); @@ -80,7 +83,7 @@ private static void CollectSamples(ArrayList samplesList) if ((j % 10) == 9) { - Console.WriteLine("; NextValue() = " + PC.NextValue().ToString()); + Console.WriteLine($"; NextValue() = {PC.NextValue()}"); OutputSample(PC.NextSample()); samplesList.Add(PC.NextSample()); } @@ -132,11 +135,11 @@ private static void CalculateResults(ArrayList samplesList) // // Example - System\ File Read Operations/sec //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ - private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1) + private static float MyComputeCounterValue(CounterSample s0, CounterSample s1) { - Single numerator = (Single)(s1.RawValue - s0.RawValue); - Single denomenator = (Single)(s1.TimeStamp - s0.TimeStamp) / (Single)s1.SystemFrequency; - Single counterValue = numerator / denomenator; + float numerator = (float)(s1.RawValue - s0.RawValue); + float denomenator = (float)(s1.TimeStamp - s0.TimeStamp) / (float)s1.SystemFrequency; + float counterValue = numerator / denomenator; return (counterValue); } diff --git a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rawfraction.cs b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rawfraction.cs index 008ab43884c..e213107eca7 100644 --- a/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rawfraction.cs +++ b/snippets/csharp/System.Diagnostics/PerformanceCounterType/Overview/rawfraction.cs @@ -10,9 +10,9 @@ public class App5 private static PerformanceCounter PC; private static PerformanceCounter BPC; - public static void Main() + public static void Run() { - ArrayList samplesList = new ArrayList(); + ArrayList samplesList = []; // If the category does not exist, create the category and exit. // Performance counters should not be created and immediately used. @@ -32,18 +32,22 @@ private static bool SetupCategory() if (!PerformanceCounterCategory.Exists("RawFractionSampleCategory")) { - CounterCreationDataCollection CCDC = new CounterCreationDataCollection(); + CounterCreationDataCollection CCDC = []; // Add the counter. - CounterCreationData rf = new CounterCreationData(); - rf.CounterType = PerformanceCounterType.RawFraction; - rf.CounterName = "RawFractionSample"; + CounterCreationData rf = new() + { + CounterType = PerformanceCounterType.RawFraction, + CounterName = "RawFractionSample" + }; CCDC.Add(rf); // Add the base counter. - CounterCreationData rfBase = new CounterCreationData(); - rfBase.CounterType = PerformanceCounterType.RawBase; - rfBase.CounterName = "RawFractionSampleBase"; + CounterCreationData rfBase = new() + { + CounterType = PerformanceCounterType.RawBase, + CounterName = "RawFractionSampleBase" + }; CCDC.Add(rfBase); // Create the category. @@ -78,7 +82,7 @@ private static void CreateCounters() private static void CollectSamples(ArrayList samplesList) { - Random r = new Random(DateTime.Now.Millisecond); + Random r = new(DateTime.Now.Millisecond); // Initialize the performance counter. PC.NextSample(); @@ -100,7 +104,7 @@ private static void CollectSamples(ArrayList samplesList) // Copy out the next value every ten times around the loop. if ((j % 10) == 9) { - Console.WriteLine("; NextValue() = " + PC.NextValue().ToString()); + Console.WriteLine($"; NextValue() = {PC.NextValue()}"); OutputSample(PC.NextSample()); samplesList.Add(PC.NextSample()); } @@ -144,11 +148,11 @@ private static void CalculateResults(ArrayList samplesList) // Average - SUM (N / D) /x // Example - Paging File\% Usage Peak //++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++//++++++++ - private static Single MyComputeCounterValue(CounterSample rfSample) + private static float MyComputeCounterValue(CounterSample rfSample) { - Single numerator = (Single)rfSample.RawValue; - Single denomenator = (Single)rfSample.BaseValue; - Single counterValue = (numerator / denomenator) * 100; + float numerator = (float)rfSample.RawValue; + float denomenator = (float)rfSample.BaseValue; + float counterValue = (numerator / denomenator) * 100; return (counterValue); } diff --git a/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/Person.cs b/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/Person.cs index cd28cd6abff..e365928a16b 100644 --- a/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/Person.cs +++ b/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/Person.cs @@ -3,45 +3,45 @@ namespace SDKSample { - // This class implements INotifyPropertyChanged - // to support one-way and two-way bindings - // (such that the UI element updates when the source - // has been changed dynamically) - public class Person : INotifyPropertyChanged - { - private string name; - // Declare the event - public event PropertyChangedEventHandler PropertyChanged; + // This class implements INotifyPropertyChanged + // to support one-way and two-way bindings + // (such that the UI element updates when the source + // has been changed dynamically) + public class Person : INotifyPropertyChanged + { + private string name; + // Declare the event + public event PropertyChangedEventHandler PropertyChanged; - public Person() - { - } + public Person() + { + } - public Person(string value) - { - this.name = value; - } - - public string PersonName - { - get { return name; } - set - { - name = value; - // Call OnPropertyChanged whenever the property is updated - OnPropertyChanged("PersonName"); - } - } + public Person(string value) + { + this.name = value; + } - // Create the OnPropertyChanged method to raise the event - protected void OnPropertyChanged(string name) - { - PropertyChangedEventHandler handler = PropertyChanged; - if (handler != null) - { - handler(this, new PropertyChangedEventArgs(name)); - } - } - } + public string PersonName + { + get { return name; } + set + { + name = value; + // Call OnPropertyChanged whenever the property is updated + OnPropertyChanged("PersonName"); + } + } + + // Create the OnPropertyChanged method to raise the event + protected void OnPropertyChanged(string name) + { + PropertyChangedEventHandler handler = PropertyChanged; + if (handler != null) + { + handler(this, new PropertyChangedEventArgs(name)); + } + } + } } -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/SimpleBinding.csproj b/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/SimpleBinding.csproj index b12ce1b3f1a..f52a7b55674 100644 --- a/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/SimpleBinding.csproj +++ b/snippets/csharp/System.Diagnostics/PresentationTraceLevel/Overview/SimpleBinding.csproj @@ -1,9 +1,7 @@ - WinExe net10.0-windows true - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/ArgsEcho/Project.csproj b/snippets/csharp/System.Diagnostics/Process/ArgsEcho/Project.csproj new file mode 100644 index 00000000000..e80cb19f82d --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/ArgsEcho/Project.csproj @@ -0,0 +1,7 @@ + + + Exe + net10.0 + ArgsEcho + + diff --git a/snippets/csharp/System.Diagnostics/Process/ArgsEcho/processstartstatic3.cs b/snippets/csharp/System.Diagnostics/Process/ArgsEcho/processstartstatic3.cs new file mode 100644 index 00000000000..4f4f4bc6746 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/ArgsEcho/processstartstatic3.cs @@ -0,0 +1,13 @@ +// This console project builds the ArgsEcho.exe target. + +using System; + +Console.WriteLine("Received the following arguments:\n"); + +for (int i = 0; i < args.Length; i++) +{ + Console.WriteLine($"[{i}] = {args[i]}"); +} + +Console.WriteLine("\nPress any key to exit"); +Console.ReadLine(); diff --git a/snippets/csharp/System.Diagnostics/Process/BasePriority/Project.csproj b/snippets/csharp/System.Diagnostics/Process/BasePriority/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/BasePriority/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/BasePriority/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/BasePriority/source.cs b/snippets/csharp/System.Diagnostics/Process/BasePriority/source.cs index 982a99e627e..5b8efeb785a 100644 --- a/snippets/csharp/System.Diagnostics/Process/BasePriority/source.cs +++ b/snippets/csharp/System.Diagnostics/Process/BasePriority/source.cs @@ -15,13 +15,13 @@ public static void Main() { // Define variables to track the peak // memory usage of the process. - long peakPagedMem = 0, + long peakPagedMem = 0, peakWorkingSet = 0, peakVirtualMem = 0; // Start the process. - using (Process myProcess = Process.Start("NotePad.exe")) { + using Process myProcess = Process.Start("NotePad.exe"); // Display the process statistics until // the user closes the program. do @@ -48,7 +48,7 @@ public static void Main() Console.WriteLine($" Paged memory size : {myProcess.PagedMemorySize64}"); // Update the values for the overall peak memory statistics. - peakPagedMem = myProcess.PeakPagedMemorySize64; + peakPagedMem = myProcess.PeakPagedMemorySize64; peakVirtualMem = myProcess.PeakVirtualMemorySize64; peakWorkingSet = myProcess.PeakWorkingSet64; diff --git a/snippets/csharp/System.Diagnostics/Process/Close/Project.csproj b/snippets/csharp/System.Diagnostics/Process/Close/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/Close/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/Close/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/Close/process_refresh.cs b/snippets/csharp/System.Diagnostics/Process/Close/process_refresh.cs index a75f00942a9..887b614e91d 100644 --- a/snippets/csharp/System.Diagnostics/Process/Close/process_refresh.cs +++ b/snippets/csharp/System.Diagnostics/Process/Close/process_refresh.cs @@ -25,31 +25,29 @@ public static void Main() { try { - using (Process myProcess = Process.Start("Notepad.exe")) + using Process myProcess = Process.Start("Notepad.exe"); + // Display physical memory usage 5 times at intervals of 2 seconds. + for (int i = 0; i < 5; i++) { - // Display physical memory usage 5 times at intervals of 2 seconds. - for (int i = 0; i < 5; i++) + if (!myProcess.HasExited) { - if (!myProcess.HasExited) - { - // Discard cached information about the process. - myProcess.Refresh(); - // Print working set to console. - Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}"); - // Wait 2 seconds. - Thread.Sleep(2000); - } - else - { - break; - } + // Discard cached information about the process. + myProcess.Refresh(); + // Print working set to console. + Console.WriteLine($"Physical Memory Usage: {myProcess.WorkingSet}"); + // Wait 2 seconds. + Thread.Sleep(2000); + } + else + { + break; } - - // Close process by sending a close message to its main window. - myProcess.CloseMainWindow(); - // Free resources associated with process. - myProcess.Close(); } + + // Close process by sending a close message to its main window. + myProcess.CloseMainWindow(); + // Free resources associated with process. + myProcess.Close(); } catch (Exception e) when (e is Win32Exception || e is FileNotFoundException) { diff --git a/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/Project.csproj b/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/processexitedevent.cs b/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/processexitedevent.cs index 97be1c65174..4e3577d05f6 100644 --- a/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/processexitedevent.cs +++ b/snippets/csharp/System.Diagnostics/Process/EnableRaisingEvents/processexitedevent.cs @@ -13,7 +13,8 @@ public async Task PrintDoc(string fileName) { eventHandled = new TaskCompletionSource(); - using (myProcess = new Process()) + using Process process = new(); + myProcess = process; { try { @@ -32,7 +33,7 @@ public async Task PrintDoc(string fileName) } // Wait for Exited event, but not more than 30 seconds. - await Task.WhenAny(eventHandled.Task,Task.Delay(30000)); + await Task.WhenAny(eventHandled.Task, Task.Delay(30000)); } } @@ -56,7 +57,7 @@ public static async Task Main(string[] args) } // Create the process and print the document. - PrintProcessClass myPrintProcess = new PrintProcessClass(); + PrintProcessClass myPrintProcess = new(); await myPrintProcess.PrintDoc(args[0]); } } diff --git a/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/Project.csproj b/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/processstaticget.cs b/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/processstaticget.cs index 7c8d993beba..15690807230 100644 --- a/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/processstaticget.cs +++ b/snippets/csharp/System.Diagnostics/Process/GetCurrentProcess/processstaticget.cs @@ -1,7 +1,7 @@ // using System; -using System.Diagnostics; using System.ComponentModel; +using System.Diagnostics; namespace MyProcessSample { @@ -42,7 +42,7 @@ void BindToRunningProcesses() static void Main() { - MyProcess myProcess = new MyProcess(); + MyProcess myProcess = new(); myProcess.BindToRunningProcesses(); } } diff --git a/snippets/csharp/System.Diagnostics/Process/Id/Project.csproj b/snippets/csharp/System.Diagnostics/Process/Id/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/Id/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/Id/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/Id/program.cs b/snippets/csharp/System.Diagnostics/Process/Id/program.cs index 7d9dad4dde4..81a402e9f6e 100644 --- a/snippets/csharp/System.Diagnostics/Process/Id/program.cs +++ b/snippets/csharp/System.Diagnostics/Process/Id/program.cs @@ -1,9 +1,7 @@ // using System; -using System.Threading; -using System.Security.Permissions; -using System.Security.Principal; using System.Diagnostics; +using System.Threading; class ProcessDemo { @@ -19,7 +17,7 @@ public static void Main() { // You can use the process Id to pass to other applications or to // reference that particular instance of the application. - Console.WriteLine(localByName[i - 1].Id.ToString()); + Console.WriteLine(localByName[i - 1].Id); i -= 1; } @@ -33,8 +31,8 @@ public static void Main() try { - using (Process chosen = Process.GetProcessById(Int32.Parse(id))) { + using Process chosen = Process.GetProcessById(int.Parse(id)); if (chosen.ProcessName == "notepad") { chosen.Kill(); @@ -42,7 +40,7 @@ public static void Main() } } } - catch (Exception e) + catch (Exception) { Console.WriteLine("Incorrect entry."); continue; diff --git a/snippets/csharp/System.Diagnostics/Process/MachineName/Project.csproj b/snippets/csharp/System.Diagnostics/Process/MachineName/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/MachineName/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/MachineName/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/MachineName/process_getprocessesbyname2_2.cs b/snippets/csharp/System.Diagnostics/Process/MachineName/process_getprocessesbyname2_2.cs index cc1d4f8d08d..323682217ff 100644 --- a/snippets/csharp/System.Diagnostics/Process/MachineName/process_getprocessesbyname2_2.cs +++ b/snippets/csharp/System.Diagnostics/Process/MachineName/process_getprocessesbyname2_2.cs @@ -33,7 +33,9 @@ public static void Main(string[] args) Process[] myProcesses = Process.GetProcessesByName("notepad", remoteMachineName); if (myProcesses.Length == 0) + { Console.WriteLine("Could not find notepad processes on remote computer."); + } foreach (Process myProcess in myProcesses) { @@ -45,7 +47,7 @@ public static void Main(string[] args) } catch (ArgumentException) { - Console.WriteLine($"The value \'{remoteMachineName}\' is an invalid remote computer name."); + Console.WriteLine($"The value '{remoteMachineName}' is an invalid remote computer name."); } catch (InvalidOperationException) { diff --git a/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/Project.csproj b/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/process_mainwindowtitle.cs b/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/process_mainwindowtitle.cs index cc9d9989e82..722ef640b7c 100644 --- a/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/process_mainwindowtitle.cs +++ b/snippets/csharp/System.Diagnostics/Process/MainWindowTitle/process_mainwindowtitle.cs @@ -14,11 +14,13 @@ public static void Main() try { // Create an instance of process component. - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Create an instance of 'myProcessStartInfo'. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(); - myProcessStartInfo.FileName = "notepad"; + ProcessStartInfo myProcessStartInfo = new() + { + FileName = "notepad" + }; myProcess.StartInfo = myProcessStartInfo; // Start process. myProcess.Start(); diff --git a/snippets/csharp/System.Diagnostics/Process/OnExited/Project.csproj b/snippets/csharp/System.Diagnostics/Process/OnExited/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/OnExited/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/OnExited/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/OnExited/program.cs b/snippets/csharp/System.Diagnostics/Process/OnExited/program.cs index a2432a101c2..f54282bf9a4 100644 --- a/snippets/csharp/System.Diagnostics/Process/OnExited/program.cs +++ b/snippets/csharp/System.Diagnostics/Process/OnExited/program.cs @@ -6,8 +6,8 @@ class MyProcess : Process { public void Stop() { - this.CloseMainWindow(); - this.Close(); + CloseMainWindow(); + Close(); OnExited(); } } @@ -16,7 +16,7 @@ class StartNotePad public static void Main(string[] args) { - MyProcess p = new MyProcess(); + MyProcess p = new(); p.StartInfo.FileName = "notepad.exe"; p.EnableRaisingEvents = true; p.Exited += new EventHandler(myProcess_HasExited); @@ -29,4 +29,4 @@ private static void myProcess_HasExited(object sender, System.EventArgs e) Console.WriteLine("Process has exited."); } } -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Diagnostics/Process/Overview/Program.cs b/snippets/csharp/System.Diagnostics/Process/Overview/Program.cs new file mode 100644 index 00000000000..c6357cf05e0 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/Overview/Program.cs @@ -0,0 +1,17 @@ +using System; + +switch (args.Length > 0 ? args[0] : null) +{ + case "instance": + MyProcessSample.MyProcessInstanceSample.Run(); + break; + case "static": + MyProcessSample.MyProcessStaticSample.Run(); + break; + case "start-args-echo": + StartArgsEcho.Program.Run(); + break; + default: + Console.WriteLine("Specify: instance, static, or start-args-echo."); + break; +} diff --git a/snippets/csharp/System.Diagnostics/Process/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/Process/Overview/Project.csproj new file mode 100644 index 00000000000..36a29620edb --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/Overview/Project.csproj @@ -0,0 +1,6 @@ + + + Exe + net10.0 + + diff --git a/snippets/csharp/System.Diagnostics/Process/Overview/processstart.cs b/snippets/csharp/System.Diagnostics/Process/Overview/processstart.cs index 33c9693e3b3..66b1f5e1bbd 100644 --- a/snippets/csharp/System.Diagnostics/Process/Overview/processstart.cs +++ b/snippets/csharp/System.Diagnostics/Process/Overview/processstart.cs @@ -1,18 +1,17 @@ // using System; using System.Diagnostics; -using System.ComponentModel; namespace MyProcessSample { - class MyProcess + class MyProcessInstanceSample { - public static void Main() + public static void Run() { try { - using (Process myProcess = new Process()) { + using Process myProcess = new(); myProcess.StartInfo.UseShellExecute = false; // You can start any process, HelloWorld is a do-nothing example. myProcess.StartInfo.FileName = "C:\\HelloWorld.exe"; diff --git a/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic.cs b/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic.cs index c47306d3811..d248f15163f 100644 --- a/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic.cs +++ b/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic.cs @@ -1,10 +1,9 @@ using System; using System.Diagnostics; -using System.ComponentModel; namespace MyProcessSample { - class MyProcess + class MyProcessStaticSample { // Opens the Internet Explorer application. void OpenApplication(string myFavoritesPath) @@ -32,8 +31,10 @@ void OpenWithArguments() // both in a minimized mode. void OpenWithStartInfo() { - ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe"); - startInfo.WindowStyle = ProcessWindowStyle.Minimized; + ProcessStartInfo startInfo = new("IExplore.exe") + { + WindowStyle = ProcessWindowStyle.Minimized + }; Process.Start(startInfo); @@ -42,13 +43,13 @@ void OpenWithStartInfo() Process.Start(startInfo); } - static void Main() + public static void Run() { // Get the path that stores favorite links. string myFavoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites); - MyProcess myProcess = new MyProcess(); + MyProcessStaticSample myProcess = new(); myProcess.OpenApplication(myFavoritesPath); myProcess.OpenWithArguments(); diff --git a/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic2.cs b/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic2.cs index ff32e39f344..b2aa7db4bc9 100644 --- a/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic2.cs +++ b/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic2.cs @@ -1,26 +1,23 @@ // Place this code into a console project called StartArgsEcho. It depends on the // console application named argsecho.exe. -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Diagnostics; namespace StartArgsEcho { class Program { - static void Main() + public static void Run() { - ProcessStartInfo startInfo = new ProcessStartInfo("argsecho.exe"); - startInfo.WindowStyle = ProcessWindowStyle.Normal; + ProcessStartInfo startInfo = new("argsecho.exe") + { + WindowStyle = ProcessWindowStyle.Normal, - // Start with one argument. - // Output of ArgsEcho: - // [0]=/a - startInfo.Arguments = "/a"; + // Start with one argument. + // Output of ArgsEcho: + // [0]=/a + Arguments = "/a" + }; Process.Start(startInfo); // Start with multiple arguments separated by spaces. diff --git a/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic3.cs b/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic3.cs deleted file mode 100644 index cdfab537ef0..00000000000 --- a/snippets/csharp/System.Diagnostics/Process/Overview/processstartstatic3.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Place this code into a console project called ArgsEcho to build the argsecho.exe target - -using System; - -namespace StartArgs -{ - class ArgsEcho - { - static void Main(string[] args) - { - Console.WriteLine("Received the following arguments:\n"); - - for (var i = 0; i < args.Length; i++) - { - Console.WriteLine($"[{i}] = {args[i]}"); - } - - Console.WriteLine("\nPress any key to exit"); - Console.ReadLine(); - } - } -} diff --git a/snippets/csharp/System.Diagnostics/Process/StandardError/Program.cs b/snippets/csharp/System.Diagnostics/Process/StandardError/Program.cs new file mode 100644 index 00000000000..63aee698e25 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/StandardError/Program.cs @@ -0,0 +1,16 @@ +using System; + +string[] sampleArgs = args.Length > 1 ? args[1..] : []; + +switch (args.Length > 0 ? args[0] : null) +{ + case "async": + Process_StandardError.Class1.Run(sampleArgs); + break; + case "sync": + Example.Run(); + break; + default: + Console.WriteLine("Specify: async or sync."); + break; +} diff --git a/snippets/csharp/System.Diagnostics/Process/StandardError/Project.csproj b/snippets/csharp/System.Diagnostics/Process/StandardError/Project.csproj index 863da0157ba..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardError/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/StandardError/Project.csproj @@ -1,9 +1,6 @@ - Exe net10.0 - Process_StandardError.Class1 - diff --git a/snippets/csharp/System.Diagnostics/Process/StandardError/source.cs b/snippets/csharp/System.Diagnostics/Process/StandardError/source.cs index 85a554a5d6e..e235df84c11 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardError/source.cs +++ b/snippets/csharp/System.Diagnostics/Process/StandardError/source.cs @@ -15,7 +15,7 @@ namespace Process_StandardError { class Class1 { - static void Main(string[] args) + public static void Run(string[] args) { if (args.Length < 1) { @@ -34,12 +34,13 @@ static void Main(string[] args) public static void GetStandardError(string[] args) { // - using (Process myProcess = new Process()) { - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("net ", "use " + args[0]); - - myProcessStartInfo.UseShellExecute = false; - myProcessStartInfo.RedirectStandardError = true; + using Process myProcess = new(); + ProcessStartInfo myProcessStartInfo = new("net", "use " + args[0]) + { + UseShellExecute = false, + RedirectStandardError = true + }; myProcess.StartInfo = myProcessStartInfo; myProcess.Start(); diff --git a/snippets/csharp/System.Diagnostics/Process/StandardError/stderror-sync.cs b/snippets/csharp/System.Diagnostics/Process/StandardError/stderror-sync.cs index c5c66aadeb0..80a5af81984 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardError/stderror-sync.cs +++ b/snippets/csharp/System.Diagnostics/Process/StandardError/stderror-sync.cs @@ -3,21 +3,23 @@ public class Example { - public static void Main() - { - var p = new Process(); - p.StartInfo.UseShellExecute = false; - p.StartInfo.RedirectStandardError = true; - p.StartInfo.FileName = "Write500Lines.exe"; - p.Start(); + public static void Run() + { + Process p = new(); + p.StartInfo.UseShellExecute = false; + p.StartInfo.RedirectStandardError = true; + p.StartInfo.FileName = "Write500Lines.exe"; + p.Start(); - // To avoid deadlocks, always read the output stream first and then wait. - string output = p.StandardError.ReadToEnd(); - p.WaitForExit(); + // To avoid deadlocks, always read the output stream first and then wait. + string output = p.StandardError.ReadToEnd(); + p.WaitForExit(); - Console.WriteLine($"\nError stream: {output}"); - } + Console.WriteLine($"\nError stream: {output}"); + } } + // The end of the output produced by the example includes the following: + // Error stream: -// Successfully wrote 500 lines. \ No newline at end of file +// Successfully wrote 500 lines. diff --git a/snippets/csharp/System.Diagnostics/Process/StandardInput/Project.csproj b/snippets/csharp/System.Diagnostics/Process/StandardInput/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardInput/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/StandardInput/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/StandardInput/process_standardinput.cs b/snippets/csharp/System.Diagnostics/Process/StandardInput/process_standardinput.cs index ec9d24568aa..8caa1a6558b 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardInput/process_standardinput.cs +++ b/snippets/csharp/System.Diagnostics/Process/StandardInput/process_standardinput.cs @@ -9,9 +9,8 @@ // using System; -using System.IO; using System.Diagnostics; -using System.ComponentModel; +using System.IO; namespace ProcessStandardInputSample { @@ -23,8 +22,8 @@ static void Main() // Start the Sort.exe process with redirected input. // Use the sort command to sort the input text. - using (Process myProcess = new Process()) { + using Process myProcess = new(); myProcess.StartInfo.FileName = "Sort.exe"; myProcess.StartInfo.UseShellExecute = false; myProcess.StartInfo.RedirectStandardInput = true; @@ -36,7 +35,7 @@ static void Main() // Prompt the user for input text lines to sort. // Write each line to the StandardInput stream of // the sort command. - String inputText; + string inputText; int numLines = 0; do { diff --git a/snippets/csharp/System.Diagnostics/Process/StandardOutput/Program.cs b/snippets/csharp/System.Diagnostics/Process/StandardOutput/Program.cs new file mode 100644 index 00000000000..46bb0fc7280 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/StandardOutput/Program.cs @@ -0,0 +1,17 @@ +using System; + +switch (args.Length > 0 ? args[0] : null) +{ + case "redirect": + StandardOutputExample.Run(); + break; + case "async": + AsyncExample.Run(); + break; + case "sync": + SyncExample.Run(); + break; + default: + Console.WriteLine("Specify: redirect, async, or sync."); + break; +} diff --git a/snippets/csharp/System.Diagnostics/Process/StandardOutput/Project.csproj b/snippets/csharp/System.Diagnostics/Process/StandardOutput/Project.csproj index 974f3738a76..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardOutput/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/StandardOutput/Project.csproj @@ -1,9 +1,6 @@ - Exe net10.0 - Example - diff --git a/snippets/csharp/System.Diagnostics/Process/StandardOutput/process_standardoutput.cs b/snippets/csharp/System.Diagnostics/Process/StandardOutput/process_standardoutput.cs index 803e2038c5c..8cc283164a9 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardOutput/process_standardoutput.cs +++ b/snippets/csharp/System.Diagnostics/Process/StandardOutput/process_standardoutput.cs @@ -1,13 +1,13 @@ -using System; -using System.IO; +using System; using System.Diagnostics; +using System.IO; class StandardOutputExample { - public static void Main() + public static void Run() { - using (Process process = new Process()) { + using Process process = new(); process.StartInfo.FileName = "ipconfig.exe"; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; @@ -26,4 +26,4 @@ public static void Main() Console.WriteLine("\n\nPress any key to exit."); Console.ReadLine(); } -} \ No newline at end of file +} diff --git a/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-async.cs b/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-async.cs index d67c699b88d..fcd2428a3eb 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-async.cs +++ b/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-async.cs @@ -1,11 +1,11 @@ using System; using System.Diagnostics; -public class Example +public class AsyncExample { - public static void Main() + public static void Run() { - var p = new Process(); + Process p = new(); p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; string eOut = null; @@ -24,7 +24,9 @@ public static void Main() Console.WriteLine($"\nError stream: {eOut}"); } } + // The example displays the following output: + // The last 50 characters in the output stream are: // 'ritten: 99,80% // Line 500 of 500 written: 100,00% diff --git a/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-sync.cs b/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-sync.cs index 69d041ee319..0896467d69e 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-sync.cs +++ b/snippets/csharp/System.Diagnostics/Process/StandardOutput/stdoutput-sync.cs @@ -1,9 +1,9 @@ using System; using System.Diagnostics; -public class Example2 +public class SyncExample { - public static void Main() + public static void Run() { var p = new Process(); p.StartInfo.UseShellExecute = false; @@ -18,7 +18,9 @@ public static void Main() Console.WriteLine($"The last 50 characters in the output stream are:\n'{output.Substring(output.Length - 50)}'"); } } + // The example displays the following output: + // Successfully wrote 500 lines. // // The last 50 characters in the output stream are: diff --git a/snippets/csharp/System.Diagnostics/Process/Start/Project.csproj b/snippets/csharp/System.Diagnostics/Process/Start/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Process/Start/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Process/Start/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Process/Start/program.cs b/snippets/csharp/System.Diagnostics/Process/Start/program.cs index 5f61a95c4b3..28002c05462 100644 --- a/snippets/csharp/System.Diagnostics/Process/Start/program.cs +++ b/snippets/csharp/System.Diagnostics/Process/Start/program.cs @@ -1,9 +1,9 @@ // // NOTE: This example requires a text.txt file file in your Documents folder using System; +using System.ComponentModel; using System.Diagnostics; using System.Security; -using System.ComponentModel; class Example { @@ -14,7 +14,7 @@ static void Main() Console.Write("Enter you user name: "); string uname = Console.ReadLine(); Console.Write("Enter your password: "); - SecureString password = new SecureString(); + SecureString password = new(); ConsoleKeyInfo key; do { diff --git a/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Program.cs b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Program.cs new file mode 100644 index 00000000000..e42ed802230 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Program.cs @@ -0,0 +1,21 @@ +using System; + +internal static class Program +{ + [STAThread] + public static void Main(string[] args) + { + switch (args.Length > 0 ? args[0] : null) + { + case "button": + ProcessSynchronizingObject.Form1.Run(); + break; + case "form": + SynchronizingObjectTest.SyncForm.Run(); + break; + default: + Console.WriteLine("Specify: button or form."); + break; + } + } +} diff --git a/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Project.csproj b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Project.csproj new file mode 100644 index 00000000000..057702ffa6e --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/Project.csproj @@ -0,0 +1,7 @@ + + + WinExe + net10.0-windows + true + + diff --git a/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/process_synchronizingobject.cs b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/process_synchronizingobject.cs index ed8b2a7a941..f172a6e1a51 100644 --- a/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/process_synchronizingobject.cs +++ b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/process_synchronizingobject.cs @@ -39,7 +39,7 @@ protected override void Dispose(bool disposing) #region Windows Form Designer generated code private void InitializeComponent() { - this.button1 = new process_SynchronizingObject.MyButton(); + button1 = new MyButton(); this.SuspendLayout(); // // button1 @@ -64,7 +64,7 @@ private void InitializeComponent() #endregion [STAThread] - static void Main() + public static void Run() { Application.Run(new Form1()); } @@ -73,9 +73,9 @@ static void Main() private MyButton button1; private void button1_Click(object sender, System.EventArgs e) { - using (Process myProcess = new Process()) { - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("mspaint"); + using Process myProcess = new(); + ProcessStartInfo myProcessStartInfo = new("mspaint"); myProcess.StartInfo = myProcessStartInfo; myProcess.Start(); myProcess.Exited += new EventHandler(MyProcessExited); @@ -88,7 +88,7 @@ private void button1_Click(object sender, System.EventArgs e) myProcess.WaitForExit(); } } - private void MyProcessExited(Object source, EventArgs e) + private void MyProcessExited(object source, EventArgs e) { MessageBox.Show("The process has exited."); } diff --git a/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/remarks.cs b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/remarks.cs index 39e9642835e..6ebe163fdb5 100644 --- a/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/remarks.cs +++ b/snippets/csharp/System.Diagnostics/Process/SynchronizingObject/remarks.cs @@ -68,7 +68,7 @@ private void InitializeComponent() #endregion [STAThread] - static void Main() + public static void Run() { Application.Run(new SyncForm()); } @@ -81,9 +81,9 @@ private void button1_Click(object sender, System.EventArgs e) this.button1.Hide(); this.label1.Show(); - using (Process process1 = new Process()) { - ProcessStartInfo process1StartInfo = new ProcessStartInfo("notepad"); + using Process process1 = new(); + ProcessStartInfo process1StartInfo = new("notepad"); // process1.StartInfo.Domain = ""; @@ -108,7 +108,7 @@ private void button1_Click(object sender, System.EventArgs e) } } - private void TheProcessExited(Object source, EventArgs e) + private void TheProcessExited(object source, EventArgs e) { this.label1.Hide(); this.button1.Show(); diff --git a/snippets/csharp/System.Diagnostics/Process/Write500Lines/Project.csproj b/snippets/csharp/System.Diagnostics/Process/Write500Lines/Project.csproj new file mode 100644 index 00000000000..07fa629cd27 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Process/Write500Lines/Project.csproj @@ -0,0 +1,7 @@ + + + Exe + net10.0 + Write500Lines + + diff --git a/snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs b/snippets/csharp/System.Diagnostics/Process/Write500Lines/write500lines.cs similarity index 53% rename from snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs rename to snippets/csharp/System.Diagnostics/Process/Write500Lines/write500lines.cs index eb4aaff9d01..1479aec1a4d 100644 --- a/snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs +++ b/snippets/csharp/System.Diagnostics/Process/Write500Lines/write500lines.cs @@ -1,16 +1,16 @@ -using System; +// This project creates the Write500Lines.exe target used by the other examples in this section. -public class Example3 -{ - public static void Main() - { - for (int ctr = 0; ctr < 500; ctr++) - Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}"); +using System; - Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n"); - } +for (int ctr = 0; ctr < 500; ctr++) +{ + Console.WriteLine($"Line {ctr + 1} of 500 written: {(ctr + 1) / 500.0:P2}"); } + +Console.Error.WriteLine("\nSuccessfully wrote 500 lines.\n"); + // The example displays the following output: + // Line 1 of 500 written: 0,20% // Line 2 of 500 written: 0,40% // Line 3 of 500 written: 0,60% diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/processmodule_baseaddress.cs b/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/processmodule_baseaddress.cs index 6234db2d8fb..d5d33f6e942 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/processmodule_baseaddress.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/BaseAddress/processmodule_baseaddress.cs @@ -15,10 +15,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/processmodule_entrypoint.cs b/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/processmodule_entrypoint.cs index 258eccaf50f..957117f0396 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/processmodule_entrypoint.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/EntryPointAddress/processmodule_entrypoint.cs @@ -15,10 +15,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/FileName/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/FileName/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/FileName/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/FileName/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/FileName/processmodule_filename.cs b/snippets/csharp/System.Diagnostics/ProcessModule/FileName/processmodule_filename.cs index 15b95e3c19a..171951808ca 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/FileName/processmodule_filename.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/FileName/processmodule_filename.cs @@ -15,10 +15,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/processmodule_fileversioninfo.cs b/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/processmodule_fileversioninfo.cs index 1d936b97dac..185f765887a 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/processmodule_fileversioninfo.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/FileVersionInfo/processmodule_fileversioninfo.cs @@ -15,10 +15,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/processmodule_modulememorysize.cs b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/processmodule_modulememorysize.cs index c631ebb5d27..94a0a4e3caa 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/processmodule_modulememorysize.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleMemorySize/processmodule_modulememorysize.cs @@ -15,10 +15,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/processmodule_modulename.cs b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/processmodule_modulename.cs index 5c571c57caf..1c025fa330c 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/processmodule_modulename.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/ModuleName/processmodule_modulename.cs @@ -15,10 +15,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/Overview/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/Overview/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/Overview/processmodule.cs b/snippets/csharp/System.Diagnostics/ProcessModule/Overview/processmodule.cs index e3c02757baa..29150b1b37f 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/Overview/processmodule.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/Overview/processmodule.cs @@ -14,10 +14,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/ToString/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessModule/ToString/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/ToString/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessModule/ToString/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessModule/ToString/processmodule_tostring.cs b/snippets/csharp/System.Diagnostics/ProcessModule/ToString/processmodule_tostring.cs index 63cfdf2c953..52ed6d3ca86 100644 --- a/snippets/csharp/System.Diagnostics/ProcessModule/ToString/processmodule_tostring.cs +++ b/snippets/csharp/System.Diagnostics/ProcessModule/ToString/processmodule_tostring.cs @@ -15,10 +15,10 @@ public static void Main() try { // - using (Process myProcess = new Process()) { + using Process myProcess = new(); // Get the process start information of notepad. - ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("notepad.exe"); + ProcessStartInfo myProcessStartInfo = new("notepad.exe"); // Assign 'StartInfo' of notepad to 'StartInfo' of 'myProcess' object. myProcess.StartInfo = myProcessStartInfo; // Create a notepad. diff --git a/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/stdstr.cs b/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/stdstr.cs index b743cf38a1e..efa2b8aa2de 100644 --- a/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/stdstr.cs +++ b/snippets/csharp/System.Diagnostics/ProcessStartInfo/RedirectStandardOutput/stdstr.cs @@ -10,8 +10,8 @@ static void Main() // an executable directly and in this case depending on it being in a PATH folder. By setting // RedirectStandardOutput to true, the output of csc.exe is directed to the Process.StandardOutput stream // which is then displayed in this console window directly. - using (Process compiler = new Process()) { + using Process compiler = new(); compiler.StartInfo.FileName = "csc.exe"; compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs"; compiler.StartInfo.UseShellExecute = false; diff --git a/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/Project.csproj index e17ba622dbc..1ef5ac9aa71 100644 --- a/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/Project.csproj @@ -1,13 +1,7 @@ - - Library + Exe net10.0-windows true - - - - - diff --git a/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/source.cs b/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/source.cs index 251f200293e..81d40b65d90 100644 --- a/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/source.cs +++ b/snippets/csharp/System.Diagnostics/ProcessStartInfo/Verb/source.cs @@ -1,8 +1,8 @@ // using System; using System.ComponentModel; -using System.IO; using System.Diagnostics; +using System.IO; using System.Windows.Forms; class ProcessInformation @@ -10,32 +10,35 @@ class ProcessInformation [STAThread] static void Main() { - OpenFileDialog openFileDialog1 = new OpenFileDialog(); - - openFileDialog1.InitialDirectory = "c:\\"; - openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; - openFileDialog1.FilterIndex = 2; - openFileDialog1.RestoreDirectory = true; - openFileDialog1.CheckFileExists = true; + OpenFileDialog openFileDialog1 = new() + { + InitialDirectory = "c:\\", + Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*", + FilterIndex = 2, + RestoreDirectory = true, + CheckFileExists = true + }; if (openFileDialog1.ShowDialog() == DialogResult.OK) { - var fileName = openFileDialog1.FileName; + string fileName = openFileDialog1.FileName; // int i = 0; - var startInfo = new ProcessStartInfo(fileName); + ProcessStartInfo startInfo = new(fileName) + { + UseShellExecute = true + }; // Display the possible verbs. - foreach (var verb in startInfo.Verbs) + foreach (string verb in startInfo.Verbs) { Console.WriteLine($" {i++}. {verb}"); } Console.Write("Select the index of the verb: "); - var indexInput = Console.ReadLine(); - int index; - if (Int32.TryParse(indexInput, out index)) + string indexInput = Console.ReadLine(); + if (int.TryParse(indexInput, out int index)) { if (index < 0 || index >= i) { @@ -43,7 +46,7 @@ static void Main() return; } - var verbToUse = startInfo.Verbs[index]; + string verbToUse = startInfo.Verbs[index]; startInfo.Verb = verbToUse; if (verbToUse.ToLower().IndexOf("printto") >= 0) @@ -52,20 +55,18 @@ static void Main() // The address must be in the form \\server\printer. // The printer address is passed as the Arguments property. Console.Write("Enter the network address of the target printer: "); - var arguments = Console.ReadLine(); + string arguments = Console.ReadLine(); startInfo.Arguments = arguments; } try { - using (var newProcess = new Process()) - { - newProcess.StartInfo = startInfo; - newProcess.Start(); + using Process newProcess = new(); + newProcess.StartInfo = startInfo; + newProcess.Start(); - Console.WriteLine($"{newProcess.ProcessName} for file {fileName} " + - $"started successfully with verb '{startInfo.Verb}'!"); - } + Console.WriteLine($"{newProcess.ProcessName} for file {fileName} " + + $"started successfully with verb '{startInfo.Verb}'!"); } catch (Win32Exception e) { diff --git a/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/Project.csproj b/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/Project.csproj +++ b/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/program.cs b/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/program.cs index 2d6e580f95e..8bf27a9db80 100644 --- a/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/program.cs +++ b/snippets/csharp/System.Diagnostics/ProcessThread/IdealProcessor/program.cs @@ -11,16 +11,18 @@ static void Main(string[] args) // Make sure there is an instance of notepad running. Process[] notepads = Process.GetProcessesByName("notepad"); if (notepads.Length == 0) + { Process.Start("notepad"); + } ProcessThreadCollection threads; //Process[] notepads; // Retrieve the Notepad processes. notepads = Process.GetProcessesByName("Notepad"); - // Get the ProcessThread collection for the first instance + // Get the ProcessThread collection for the first instance. threads = notepads[0].Threads; - // Set the properties on the first ProcessThread in the collection + // Set the properties on the first ProcessThread in the collection. threads[0].IdealProcessor = 0; - threads[0].ProcessorAffinity = (IntPtr)1; + threads[0].ProcessorAffinity = (nint)1; } } } diff --git a/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/Project.csproj b/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/Project.csproj index e17ba622dbc..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/Project.csproj +++ b/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/Project.csproj @@ -1,13 +1,6 @@ - - Library - net10.0-windows - true + Exe + net10.0 - - - - - diff --git a/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/program.cs b/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/program.cs index 7fa5323ac30..6993661b51a 100644 --- a/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/program.cs +++ b/snippets/csharp/System.Diagnostics/SourceFilter/.ctor/program.cs @@ -40,9 +40,8 @@ using System; using System.Collections; using System.Diagnostics; -using System.Reflection; using System.IO; -using System.Security.Permissions; +using System.Reflection; namespace Testing { @@ -52,9 +51,9 @@ class TraceTest const string DELIMITEDFILE = "DelimitedListOutput.log"; const string TESTLISTENERFILE = "C:\\Temp\\TestListener.txt"; // - public static TraceSwitch traceSwitch = new TraceSwitch("TraceSwitch", "Verbose"); - public static BooleanSwitch boolSwitch = new BooleanSwitch("BoolSwitch", "True"); - public static SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose"); + public static TraceSwitch traceSwitch = new("TraceSwitch", "Verbose"); + public static BooleanSwitch boolSwitch = new("BoolSwitch", "True"); + public static SourceSwitch sourceSwitch = new("SourceSwitch", "Verbose"); // static void Main() { @@ -64,7 +63,7 @@ static void Main() // Initialize trace switches. // #if(!ConfigFile) - TraceSwitch traceSwitch = new TraceSwitch("TraceSwitch", "Verbose"); + TraceSwitch traceSwitch = new("TraceSwitch", "Verbose"); #endif // // @@ -72,7 +71,7 @@ static void Main() // // #if(!ConfigFile) - BooleanSwitch boolSwitch = new BooleanSwitch("BoolSwitch", "True"); + BooleanSwitch boolSwitch = new("BoolSwitch", "True"); #endif // // @@ -80,7 +79,7 @@ static void Main() // // #if(!ConfigFile) - SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose"); + SourceSwitch sourceSwitch = new("SourceSwitch", "Verbose"); #endif // // @@ -88,7 +87,7 @@ static void Main() // // Initialize trace source. // - MyTraceSource ts = new MyTraceSource("TraceTest"); + MyTraceSource ts = new("TraceTest"); // // Console.WriteLine(ts.Switch.DisplayName); @@ -107,11 +106,11 @@ static void Main() // // // Display the SwitchLevelAttribute for the BooleanSwitch. - Object[] attribs = typeof(BooleanSwitch).GetCustomAttributes(typeof(SwitchLevelAttribute), false); + object[] attribs = typeof(BooleanSwitch).GetCustomAttributes(typeof(SwitchLevelAttribute), false); if (attribs.Length == 0) Console.WriteLine("Error, couldn't find SwitchLevelAttribute on BooleanSwitch."); else - Console.WriteLine(((SwitchLevelAttribute)attribs[0]).SwitchLevelType.ToString()); + Console.WriteLine(((SwitchLevelAttribute)attribs[0]).SwitchLevelType); // #if(ConfigFile) // @@ -192,7 +191,7 @@ static void Main() ts.TraceData(TraceEventType.Warning, 9, new object()); // // - ts.TraceData(TraceEventType.Warning, 10, new object[] { "Message 1", "Message 2" }); + ts.TraceData(TraceEventType.Warning, 10, ["Message 1", "Message 2"]); // // Activity tests. // @@ -214,7 +213,7 @@ static void Main() try { Console.WriteLine("Examining " + TEXTFILE); - StreamReader logfile = new StreamReader(TEXTFILE); + StreamReader logfile = new(TEXTFILE); while (!logfile.EndOfStream) Console.WriteLine(logfile.ReadLine()); @@ -233,7 +232,7 @@ static void Main() try { Console.WriteLine("Examining " + DELIMITEDFILE); - StreamReader logfile = new StreamReader(DELIMITEDFILE); + StreamReader logfile = new(DELIMITEDFILE); while (!logfile.EndOfStream) Console.WriteLine(logfile.ReadLine()); logfile.Close(); @@ -250,7 +249,7 @@ static void Main() catch (Exception e) { // Catch any unexpected exception. - Console.WriteLine("Unexpected exception: " + e.ToString()); + Console.WriteLine($"Unexpected exception: {e}"); Console.Read(); } } @@ -261,14 +260,15 @@ public class MyTraceSource : TraceSource { string firstAttribute = ""; string secondAttribute = ""; - public MyTraceSource(string n) : base(n) {} + public MyTraceSource(string n) : base(n) { } public string FirstTraceSourceAttribute { - get { + get + { foreach (DictionaryEntry de in this.Attributes) if (de.Key.ToString().ToLower() == "firsttracesourceattribute") - firstAttribute = de.Value.ToString() ; + firstAttribute = de.Value.ToString(); return firstAttribute; } set { firstAttribute = value; } @@ -276,18 +276,20 @@ public string FirstTraceSourceAttribute public string SecondTraceSourceAttribute { - get { + get + { foreach (DictionaryEntry de in this.Attributes) if (de.Key.ToString().ToLower() == "secondtracesourceattribute") secondAttribute = de.Value.ToString(); - return secondAttribute; } + return secondAttribute; + } set { secondAttribute = value; } } protected override string[] GetSupportedAttributes() { // Allow the use of the attributes in the configuration file. - return new string[] { "FirstTraceSourceAttribute", "SecondTraceSourceAttribute" }; + return ["FirstTraceSourceAttribute", "SecondTraceSourceAttribute"]; } } // @@ -311,18 +313,17 @@ public int CustomSourceSwitchAttribute protected override string[] GetSupportedAttributes() { - return new string[] { "customsourceSwitchattribute" }; + return ["customsourceSwitchattribute"]; } } // // Very basic test listener derived from TextWriterTraceListener. // - [HostProtection(Synchronization = true)] public class TestListener : TextWriterTraceListener { public TestListener(string fileName) : base(fileName) { } - public TestListener(string fileName, string name) : base(fileName, name) {} + public TestListener(string fileName, string name) : base(fileName, name) { } public override void Write(string s) { @@ -336,7 +337,7 @@ protected override string[] GetSupportedAttributes() { // The following string array will allow the use of // the name "customListenerAttribute" in the configuration file. - return new string[] { "customListenerAttribute" }; + return ["customListenerAttribute"]; } } // diff --git a/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/Project.csproj b/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/Project.csproj index e17ba622dbc..bee24992f68 100644 --- a/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/Project.csproj +++ b/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/Project.csproj @@ -1,13 +1,9 @@ - - Library - net10.0-windows - true + Exe + net10.0 - - + - diff --git a/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/stacktracesample3.cs b/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/stacktracesample3.cs index 57e218b8ecc..c3eb14f4156 100644 --- a/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/stacktracesample3.cs +++ b/snippets/csharp/System.Diagnostics/StackFrame/GetMethod/stacktracesample3.cs @@ -6,16 +6,16 @@ class MyConsoleApp [STAThread] static void Main(string[] args) { - MyConsoleApp myApp = new MyConsoleApp(); + MyConsoleApp myApp = new(); myApp.MyPublicMethod(); } public void MyPublicMethod() { - MyInnerClass helperClass = new MyInnerClass(); + MyInnerClass helperClass = new(); helperClass.ThrowsException(); } -// + // class MyInnerClass { public void ThrowsException() @@ -31,8 +31,8 @@ public void ThrowsException() // exception output by limiting the trace to the // frame of the calling method. // - StackFrame fr = new StackFrame(1,true); - StackTrace st = new StackTrace(fr); + StackFrame fr = new(1, true); + StackTrace st = new(fr); EventLog.WriteEntry(fr.GetMethod().Name, st.ToString(), EventLogEntryType.Warning); @@ -57,5 +57,5 @@ public void ThrowsException() } } } -// -} \ No newline at end of file + // +} diff --git a/snippets/csharp/System.Diagnostics/StackFrame/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/StackFrame/Overview/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/StackFrame/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/StackFrame/Overview/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/StackFrame/Overview/program.cs b/snippets/csharp/System.Diagnostics/StackFrame/Overview/program.cs index 47fd9bd1959..f0886e38453 100644 --- a/snippets/csharp/System.Diagnostics/StackFrame/Overview/program.cs +++ b/snippets/csharp/System.Diagnostics/StackFrame/Overview/program.cs @@ -6,16 +6,16 @@ namespace StackFrameExample { class Program { - static void Main(string[] args) + public static void Run(string[] args) { try { Method1(); } - catch (Exception e) + catch (Exception) { - StackTrace st = new StackTrace(); - StackTrace st1 = new StackTrace(new StackFrame(true)); + StackTrace st = new(); + StackTrace st1 = new(new StackFrame(true)); Console.WriteLine(" Stack trace for Main: {0}", st1.ToString()); Console.WriteLine(st.ToString()); @@ -29,37 +29,56 @@ private static void Method1() { Method2(4); } - catch (Exception e) + catch (Exception) { - StackTrace st = new StackTrace(); - StackTrace st1 = new StackTrace(new StackFrame(true)); + StackTrace st = new(); + StackTrace st1 = new(new StackFrame(true)); Console.WriteLine(" Stack trace for Method1: {0}", st1.ToString()); Console.WriteLine(st.ToString()); // Build a stack trace for the next frame. - StackTrace st2 = new StackTrace(new StackFrame(1, true)); + StackTrace st2 = new(new StackFrame(1, true)); Console.WriteLine(" Stack trace for next level frame: {0}", st2.ToString()); - throw e; + throw; } } - private static void Method2( int count) + private static void Method2(int count) { try { if (count < 5) throw new ArgumentException("count too large", "count"); } - catch (Exception e) + catch (Exception) { - StackTrace st = new StackTrace(); - StackTrace st1 = new StackTrace(new StackFrame(2,true)); + StackTrace st = new(); + StackTrace st1 = new(new StackFrame(2, true)); Console.WriteLine(" Stack trace for Method2: {0}", st1.ToString()); Console.WriteLine(st.ToString()); - throw e; + throw; } } } } // + +internal static class SampleRunner +{ + public static void Main(string[] args) + { + switch (args.Length > 0 ? args[0] : null) + { + case "overview": + StackFrameExample.Program.Run(args[1..]); + break; + case "levels": + SamplePublic.ConsoleApp.Run(); + break; + default: + Console.WriteLine("Specify: overview or levels."); + break; + } + } +} diff --git a/snippets/csharp/System.Diagnostics/StackFrame/Overview/source.cs b/snippets/csharp/System.Diagnostics/StackFrame/Overview/source.cs index 016f448761c..6afbeb0ab8c 100644 --- a/snippets/csharp/System.Diagnostics/StackFrame/Overview/source.cs +++ b/snippets/csharp/System.Diagnostics/StackFrame/Overview/source.cs @@ -11,253 +11,255 @@ namespace SamplePublic // of the StackTrace and StackFrame classes. class ConsoleApp { -// - [STAThread] - static void Main() + // + [STAThread] + public static void Run() { - ClassLevel1 mainClass = new ClassLevel1(); + ClassLevel1 mainClass = new(); - try { + try + { mainClass.InternalMethod(); } - catch (Exception) { - Console.WriteLine(" Main method exception handler"); + catch (Exception) + { + Console.WriteLine(" Main method exception handler"); - // Display file and line information, if available. - StackTrace st = new StackTrace(new StackFrame(true)); - Console.WriteLine(" Stack trace for current level: {0}", - st.ToString()); - Console.WriteLine(" File: {0}", - st.GetFrame(0).GetFileName()); - Console.WriteLine(" Line Number: {0}", - st.GetFrame(0).GetFileLineNumber().ToString()); + // Display file and line information, if available. + StackTrace st = new(new StackFrame(true)); + Console.WriteLine(" Stack trace for current level: {0}", + st.ToString()); + Console.WriteLine(" File: {0}", + st.GetFrame(0).GetFileName()); + Console.WriteLine(" Line Number: {0}", + st.GetFrame(0).GetFileLineNumber()); - Console.WriteLine(); - Console.WriteLine("-------------------------------------------------\n"); + Console.WriteLine(); + Console.WriteLine("-------------------------------------------------\n"); } } - // + // } } namespace SampleInternal { - public class ClassLevel1 - { -// - public void InternalMethod() - { - try - { - ClassLevel2 nestedClass = new ClassLevel2(); - nestedClass.Level2Method(); - } - catch (Exception e) - { - Console.WriteLine(" InternalMethod exception handler"); + public class ClassLevel1 + { + // + public void InternalMethod() + { + try + { + ClassLevel2 nestedClass = new(); + nestedClass.Level2Method(); + } + catch (Exception) + { + Console.WriteLine(" InternalMethod exception handler"); - // Build a stack trace from one frame, skipping the - // current frame and using the next frame. By - // default, file and line information are not displayed. - StackTrace st = new StackTrace(new StackFrame(1)); - Console.WriteLine(" Stack trace for next level frame: {0}", - st.ToString()); - Console.WriteLine(" Stack frame for next level: "); - Console.WriteLine(" {0}", st.GetFrame(0).ToString()); + // Build a stack trace from one frame, skipping the + // current frame and using the next frame. By + // default, file and line information are not displayed. + StackTrace st = new(new StackFrame(1)); + Console.WriteLine(" Stack trace for next level frame: {0}", + st.ToString()); + Console.WriteLine(" Stack frame for next level: "); + Console.WriteLine(" {0}", st.GetFrame(0).ToString()); - Console.WriteLine(" Line Number: {0}", - st.GetFrame(0).GetFileLineNumber().ToString()); + Console.WriteLine(" Line Number: {0}", + st.GetFrame(0).GetFileLineNumber()); - Console.WriteLine(); - Console.WriteLine(" ... throwing exception to next level ..."); - Console.WriteLine("-------------------------------------------------\n"); - throw e; - } - } - // - } + Console.WriteLine(); + Console.WriteLine(" ... throwing exception to next level ..."); + Console.WriteLine("-------------------------------------------------\n"); + throw; + } + } + // + } - public class ClassLevel2 - { -// - public void Level2Method() - { - try - { - ClassLevel3 nestedClass = new ClassLevel3(); - nestedClass.Level3Method(); - } - catch (Exception e) - { - Console.WriteLine(" Level2Method exception handler"); + public class ClassLevel2 + { + // + public void Level2Method() + { + try + { + ClassLevel3 nestedClass = new(); + nestedClass.Level3Method(); + } + catch (Exception) + { + Console.WriteLine(" Level2Method exception handler"); - // Display the full call stack at this level. - StackTrace st1 = new StackTrace(true); - Console.WriteLine(" Stack trace for this level: {0}", - st1.ToString()); + // Display the full call stack at this level. + StackTrace st1 = new(true); + Console.WriteLine(" Stack trace for this level: {0}", + st1.ToString()); - // Build a stack trace from one frame, skipping the current - // frame and using the next frame. - StackTrace st2 = new StackTrace(new StackFrame(1, true)); - Console.WriteLine(" Stack trace built with next level frame: {0}", - st2.ToString()); + // Build a stack trace from one frame, skipping the current + // frame and using the next frame. + StackTrace st2 = new(new StackFrame(1, true)); + Console.WriteLine(" Stack trace built with next level frame: {0}", + st2.ToString()); - // Build a stack trace skipping the current frame, and - // including all the other frames. - StackTrace st3 = new StackTrace(1, true); - Console.WriteLine(" Stack trace built from the next level up: {0}", - st3.ToString()); + // Build a stack trace skipping the current frame, and + // including all the other frames. + StackTrace st3 = new(1, true); + Console.WriteLine(" Stack trace built from the next level up: {0}", + st3.ToString()); - Console.WriteLine(); - Console.WriteLine(" ... throwing exception to next level ..."); - Console.WriteLine("-------------------------------------------------\n"); - throw e; - } - } - // - } + Console.WriteLine(); + Console.WriteLine(" ... throwing exception to next level ..."); + Console.WriteLine("-------------------------------------------------\n"); + throw; + } + } + // + } + + public class ClassLevel3 + { + // + public void Level3Method() + { + try + { + ClassLevel4 nestedClass = new(); + nestedClass.Level4Method(); + } + catch (Exception) + { + Console.WriteLine(" Level3Method exception handler"); - public class ClassLevel3 - { -// - public void Level3Method() - { - try - { - ClassLevel4 nestedClass = new ClassLevel4(); - nestedClass.Level4Method(); - } - catch (Exception e) - { - Console.WriteLine(" Level3Method exception handler"); + // Build a stack trace from a dummy stack frame. + // Explicitly specify the source file name and + // line number. + StackTrace st = new(new StackFrame("source.cs", 60)); + Console.WriteLine(" Stack trace with dummy stack frame: {0}", + st.ToString()); + for (int i = 0; i < st.FrameCount; i++) + { + // + // Display the stack frame properties. + StackFrame sf = st.GetFrame(i); + Console.WriteLine(" File: {0}", sf.GetFileName()); + Console.WriteLine(" Line Number: {0}", + sf.GetFileLineNumber()); + // Note that the column number defaults to zero + // when not initialized. + Console.WriteLine(" Column Number: {0}", + sf.GetFileColumnNumber()); + if (sf.GetILOffset() != StackFrame.OFFSET_UNKNOWN) + { + Console.WriteLine(" Intermediate Language Offset: {0}", + sf.GetILOffset()); + } + if (sf.GetNativeOffset() != StackFrame.OFFSET_UNKNOWN) + { + Console.WriteLine(" Native Offset: {0}", + sf.GetNativeOffset()); + } + // + } + Console.WriteLine(); + Console.WriteLine(" ... throwing exception to next level ..."); + Console.WriteLine("-------------------------------------------------\n"); + throw; + } + } + // + } - // Build a stack trace from a dummy stack frame. - // Explicitly specify the source file name and - // line number. - StackTrace st = new StackTrace(new StackFrame("source.cs", 60)); - Console.WriteLine(" Stack trace with dummy stack frame: {0}", - st.ToString()); - for(int i =0; i< st.FrameCount; i++ ) + public class ClassLevel4 + { + public void Level4Method() + { + // + try { -// - // Display the stack frame properties. - StackFrame sf = st.GetFrame(i); - Console.WriteLine(" File: {0}", sf.GetFileName()); - Console.WriteLine(" Line Number: {0}", - sf.GetFileLineNumber()); - // Note that the column number defaults to zero - // when not initialized. - Console.WriteLine(" Column Number: {0}", - sf.GetFileColumnNumber()); - if (sf.GetILOffset() != StackFrame.OFFSET_UNKNOWN) - { - Console.WriteLine(" Intermediate Language Offset: {0}", - sf.GetILOffset()); - } - if (sf.GetNativeOffset() != StackFrame.OFFSET_UNKNOWN) - { - Console.WriteLine(" Native Offset: {0}", - sf.GetNativeOffset()); - } -// + ClassLevel5 nestedClass = new(); + nestedClass.Level5Method(); } - Console.WriteLine(); - Console.WriteLine(" ... throwing exception to next level ..."); - Console.WriteLine("-------------------------------------------------\n"); - throw e; - } - } - // - } + catch (Exception) + { + Console.WriteLine(" Level4Method exception handler"); - public class ClassLevel4 - { - public void Level4Method() - { -// - try - { - ClassLevel5 nestedClass = new ClassLevel5(); - nestedClass.Level5Method(); - } - catch (Exception e) - { - Console.WriteLine(" Level4Method exception handler"); + // Build a stack trace from a dummy stack frame. + // Explicitly specify the source file name, line number + // and column number. + StackTrace st = new(new StackFrame("source.cs", 79, 24)); + Console.WriteLine(" Stack trace with dummy stack frame: {0}", + st.ToString()); - // Build a stack trace from a dummy stack frame. - // Explicitly specify the source file name, line number - // and column number. - StackTrace st = new StackTrace(new StackFrame("source.cs", 79, 24)); - Console.WriteLine(" Stack trace with dummy stack frame: {0}", - st.ToString()); + // Access the StackFrames explicitly to display the file + // name, line number and column number properties. + // StackTrace.ToString only includes the method name. + for (int i = 0; i < st.FrameCount; i++) + { + StackFrame sf = st.GetFrame(i); + Console.WriteLine(" File: {0}", sf.GetFileName()); + Console.WriteLine(" Line Number: {0}", + sf.GetFileLineNumber()); + Console.WriteLine(" Column Number: {0}", + sf.GetFileColumnNumber()); + } + Console.WriteLine(); + Console.WriteLine(" ... throwing exception to next level ..."); + Console.WriteLine("-------------------------------------------------\n"); + throw; + } + // + } + } - // Access the StackFrames explicitly to display the file - // name, line number and column number properties. - // StackTrace.ToString only includes the method name. - for(int i =0; i< st.FrameCount; i++ ) + public class ClassLevel5 + { + // + public void Level5Method() + { + try { - StackFrame sf = st.GetFrame(i); - Console.WriteLine(" File: {0}", sf.GetFileName()); - Console.WriteLine(" Line Number: {0}", - sf.GetFileLineNumber()); - Console.WriteLine(" Column Number: {0}", - sf.GetFileColumnNumber()); + ClassLevel6 nestedClass = new(); + nestedClass.Level6Method(); } - Console.WriteLine(); - Console.WriteLine(" ... throwing exception to next level ..."); - Console.WriteLine("-------------------------------------------------\n"); - throw e; - } -// - } - } + catch (Exception) + { + Console.WriteLine(" Level5Method exception handler"); - public class ClassLevel5 - { -// - public void Level5Method() - { - try - { - ClassLevel6 nestedClass = new ClassLevel6(); - nestedClass.Level6Method(); - } - catch (Exception e) - { - Console.WriteLine(" Level5Method exception handler"); + StackTrace st = new(); - StackTrace st = new StackTrace(); + // Display the most recent function call. + StackFrame sf = st.GetFrame(0); + Console.WriteLine(); + Console.WriteLine(" Exception in method: "); + Console.WriteLine(" {0}", sf.GetMethod()); - // Display the most recent function call. - StackFrame sf = st.GetFrame(0); - Console.WriteLine(); - Console.WriteLine(" Exception in method: "); - Console.WriteLine(" {0}", sf.GetMethod()); + if (st.FrameCount > 1) + { + // Display the highest-level function call + // in the trace. + sf = st.GetFrame(st.FrameCount - 1); + Console.WriteLine(" Original function call at top of call stack):"); + Console.WriteLine(" {0}", sf.GetMethod()); + } - if (st.FrameCount >1) - { - // Display the highest-level function call - // in the trace. - sf = st.GetFrame(st.FrameCount-1); - Console.WriteLine(" Original function call at top of call stack):"); - Console.WriteLine(" {0}", sf.GetMethod()); + Console.WriteLine(); + Console.WriteLine(" ... throwing exception to next level ..."); + Console.WriteLine("-------------------------------------------------\n"); + throw; } + } + // + } - Console.WriteLine(); - Console.WriteLine(" ... throwing exception to next level ..."); - Console.WriteLine("-------------------------------------------------\n"); - throw e; - } - } -// - } - - public class ClassLevel6 - { - public void Level6Method() - { - throw new Exception("An error occurred in the lowest internal class method."); - } - } + public class ClassLevel6 + { + public void Level6Method() + { + throw new Exception("An error occurred in the lowest internal class method."); + } + } } -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/Project.csproj b/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/Project.csproj +++ b/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/stacktracesample2.cs b/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/stacktracesample2.cs index 38b38a837a5..1bf32247244 100644 --- a/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/stacktracesample2.cs +++ b/snippets/csharp/System.Diagnostics/StackTrace/GetFrames/stacktracesample2.cs @@ -7,13 +7,13 @@ class MyConsoleApp [STAThread] static void Main() { - MyConsoleApp myApp = new MyConsoleApp(); + MyConsoleApp myApp = new(); myApp.MyPublicMethod(); } public void MyPublicMethod() { - MyInnerClass helperClass = new MyInnerClass(); + MyInnerClass helperClass = new(); helperClass.ThrowsException(); } @@ -21,10 +21,12 @@ class MyInnerClass { public void ThrowsException() { - try { + try + { throw new Exception("A problem was encountered."); } - catch (Exception) { + catch (Exception) + { // Create a StackTrace starting at the next level // stack frame. Skip the first frame, the frame of @@ -32,15 +34,15 @@ public void ThrowsException() // of the ThrowsException method. Include the line // number, file name, and column number information // for each frame. -// - StackTrace st = new StackTrace(1, true); - StackFrame [] stFrames = st.GetFrames(); + // + StackTrace st = new(1, true); + StackFrame[] stFrames = st.GetFrames(); - foreach(StackFrame sf in stFrames ) + foreach (StackFrame sf in stFrames) { - Console.WriteLine("Method: {0}", sf.GetMethod() ); + Console.WriteLine("Method: {0}", sf.GetMethod()); } -// + // } } } diff --git a/snippets/csharp/System.Diagnostics/StackTrace/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/StackTrace/Overview/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/StackTrace/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/StackTrace/Overview/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/StackTrace/Overview/stacktracesample1.cs b/snippets/csharp/System.Diagnostics/StackTrace/Overview/stacktracesample1.cs index 50f39d3f0e6..994b8fa7701 100644 --- a/snippets/csharp/System.Diagnostics/StackTrace/Overview/stacktracesample1.cs +++ b/snippets/csharp/System.Diagnostics/StackTrace/Overview/stacktracesample1.cs @@ -7,7 +7,7 @@ class StackTraceSample [STAThread] static void Main(string[] args) { - StackTraceSample sample = new StackTraceSample(); + StackTraceSample sample = new(); try { sample.MyPublicMethod(); @@ -17,8 +17,8 @@ static void Main(string[] args) // Create a StackTrace that captures // filename, line number, and column // information for the current thread. - StackTrace st = new StackTrace(true); - for(int i =0; i< st.FrameCount; i++ ) + StackTrace st = new(true); + for (int i = 0; i < st.FrameCount; i++) { // Note that high up the call stack, there is only // one stack frame. @@ -33,14 +33,14 @@ static void Main(string[] args) } } - public void MyPublicMethod () + public void MyPublicMethod() { MyProtectedMethod(); } - protected void MyProtectedMethod () + protected void MyProtectedMethod() { - MyInternalClass mic = new MyInternalClass(); + MyInternalClass mic = new(); mic.ThrowsException(); } @@ -52,27 +52,27 @@ public void ThrowsException() { throw new Exception("A problem was encountered."); } - catch (Exception e) + catch (Exception) { // Create a StackTrace that captures filename, // line number and column information. - StackTrace st = new StackTrace(true); + StackTrace st = new(true); string stackIndent = ""; - for(int i =0; i< st.FrameCount; i++ ) + for (int i = 0; i < st.FrameCount; i++) { // Note that at this level, there are four // stack frames, one for each method invocation. StackFrame sf = st.GetFrame(i); Console.WriteLine(); Console.WriteLine(stackIndent + " Method: {0}", - sf.GetMethod() ); - Console.WriteLine( stackIndent + " File: {0}", + sf.GetMethod()); + Console.WriteLine(stackIndent + " File: {0}", sf.GetFileName()); - Console.WriteLine( stackIndent + " Line Number: {0}", + Console.WriteLine(stackIndent + " Line Number: {0}", sf.GetFileLineNumber()); stackIndent += " "; } - throw e; + throw; } } } @@ -117,4 +117,4 @@ compiled with the Release configuration. High up the call stack, Line Number: 0 */ -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Diagnostics/Stopwatch/Overview/Program.cs b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/Program.cs new file mode 100644 index 00000000000..0b7d94ae6ca --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/Program.cs @@ -0,0 +1,22 @@ +using System; + +internal static class SampleRunner +{ + public static void Main(string[] args) + { + string[] sampleArgs = args.Length > 1 ? args[1..] : []; + + switch (args.Length > 0 ? args[0] : null) + { + case "operations": + StopWatchSample.OperationsTimer.Run(); + break; + case "elapsed": + Program.Run(sampleArgs); + break; + default: + Console.WriteLine("Specify: operations or elapsed."); + break; + } + } +} diff --git a/snippets/csharp/System.Diagnostics/Stopwatch/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Stopwatch/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source.cs b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source.cs index 8c3191da1ef..cb709908fd4 100644 --- a/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source.cs +++ b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source.cs @@ -8,7 +8,7 @@ namespace StopWatchSample { class OperationsTimer { - public static void Main() + public static void Run() { DisplayTimerProperties(); @@ -36,7 +36,7 @@ public static void DisplayTimerProperties() long frequency = Stopwatch.Frequency; Console.WriteLine(" Timer frequency in ticks per second = {0}", frequency); - long nanosecPerTick = (1000L*1000L*1000L) / frequency; + long nanosecPerTick = (1000L * 1000L * 1000L) / frequency; Console.WriteLine(" Timer is accurate within {0} nanoseconds", nanosecPerTick); } @@ -45,14 +45,17 @@ public static void DisplayTimerProperties() // private static void TimeOperations() { - long nanosecPerTick = (1000L*1000L*1000L) / Stopwatch.Frequency; + long nanosecPerTick = (1000L * 1000L * 1000L) / Stopwatch.Frequency; const long numIterations = 10000; // Define the operation title names. - String [] operationNames = {"Operation: Int32.Parse(\"0\")", - "Operation: Int32.TryParse(\"0\")", - "Operation: Int32.Parse(\"a\")", - "Operation: Int32.TryParse(\"a\")"}; + string[] operationNames = + [ + "Operation: Int32.Parse(\"0\")", + "Operation: Int32.TryParse(\"0\")", + "Operation: Int32.Parse(\"a\")", + "Operation: Int32.TryParse(\"a\")" + ]; // Time four different implementations for parsing // an integer from a string. @@ -64,7 +67,7 @@ private static void TimeOperations() long numTicks = 0; long numRollovers = 0; long maxTicks = 0; - long minTicks = Int64.MaxValue; + long minTicks = long.MaxValue; int indexFastest = -1; int indexSlowest = -1; long milliSec = 0; @@ -75,7 +78,7 @@ private static void TimeOperations() // The first execution time will be tossed // out, since it can skew the average time. - for (int i=0; i<=numIterations; i++) + for (int i = 0; i <= numIterations; i++) { // long ticksThisTime = 0; @@ -93,7 +96,7 @@ private static void TimeOperations() try { - inputNum = Int32.Parse("0"); + inputNum = int.Parse("0"); } catch (FormatException) { @@ -113,7 +116,7 @@ private static void TimeOperations() // Start a new stopwatch timer. timePerParse = Stopwatch.StartNew(); - if (!Int32.TryParse("0", out inputNum)) + if (!int.TryParse("0", out inputNum)) { inputNum = 0; } @@ -132,7 +135,7 @@ private static void TimeOperations() try { - inputNum = Int32.Parse("a"); + inputNum = int.Parse("a"); } catch (FormatException) { @@ -151,7 +154,7 @@ private static void TimeOperations() // Start a new stopwatch timer. timePerParse = Stopwatch.StartNew(); - if (!Int32.TryParse("a", out inputNum)) + if (!int.TryParse("a", out inputNum)) { inputNum = 0; } @@ -194,7 +197,7 @@ private static void TimeOperations() if (numTicks < ticksThisTime) { // Keep track of rollovers. - numRollovers ++; + numRollovers++; } } } @@ -212,14 +215,14 @@ private static void TimeOperations() indexFastest, numIterations, minTicks); Console.WriteLine(" Average time: {0} ticks = {1} nanoseconds", numTicks / numIterations, - (numTicks * nanosecPerTick) / numIterations ); + (numTicks * nanosecPerTick) / numIterations); Console.WriteLine(" Total time looping through {0} operations: {1} milliseconds", numIterations, milliSec); // } } // - } + } } // diff --git a/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source1.cs b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source1.cs index ee44643a109..d8dfe3c64a9 100644 --- a/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source1.cs +++ b/snippets/csharp/System.Diagnostics/Stopwatch/Overview/source1.cs @@ -4,9 +4,9 @@ using System.Threading; class Program { - static void Main(string[] args) + public static void Run(string[] args) { - Stopwatch stopWatch = new Stopwatch(); + Stopwatch stopWatch = new(); stopWatch.Start(); Thread.Sleep(10000); stopWatch.Stop(); @@ -14,10 +14,9 @@ static void Main(string[] args) TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. - string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", - ts.Hours, ts.Minutes, ts.Seconds, - ts.Milliseconds / 10); - Console.WriteLine("RunTime " + elapsedTime); + string elapsedTime = + $"{ts.Hours:00}:{ts.Minutes:00}:{ts.Seconds:00}.{ts.Milliseconds / 10:00}"; + Console.WriteLine($"RunTime {elapsedTime}"); } } // diff --git a/snippets/csharp/System.Diagnostics/Switch/Overview/Program.cs b/snippets/csharp/System.Diagnostics/Switch/Overview/Program.cs new file mode 100644 index 00000000000..3a21d5dfaa7 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Switch/Overview/Program.cs @@ -0,0 +1,14 @@ +using System; + +switch (args.Length > 0 ? args[0] : null) +{ + case "remarks": + SomeClass.Run(); + break; + case "overview": + Class1.Run(); + break; + default: + Console.WriteLine("Specify: remarks or overview."); + break; +} diff --git a/snippets/csharp/System.Diagnostics/Switch/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/Switch/Overview/Project.csproj index cd07225f0a6..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Switch/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Switch/Overview/Project.csproj @@ -1,9 +1,6 @@ - Exe net10.0 - SomeClass - diff --git a/snippets/csharp/System.Diagnostics/Switch/Overview/remarks.cs b/snippets/csharp/System.Diagnostics/Switch/Overview/remarks.cs index f387c9f4b16..438b5ea7cf7 100644 --- a/snippets/csharp/System.Diagnostics/Switch/Overview/remarks.cs +++ b/snippets/csharp/System.Diagnostics/Switch/Overview/remarks.cs @@ -4,14 +4,14 @@ public class SomeClass { // - private static BooleanSwitch boolSwitch = new BooleanSwitch("mySwitch", + private static BooleanSwitch boolSwitch = new("mySwitch", "Switch in config file"); - public static void Main() + public static void Run() { //... Console.WriteLine("Boolean switch {0} configured as {1}", - boolSwitch.DisplayName, boolSwitch.Enabled.ToString()); + boolSwitch.DisplayName, boolSwitch.Enabled); if (boolSwitch.Enabled) { //... diff --git a/snippets/csharp/System.Diagnostics/Switch/Overview/source.cs b/snippets/csharp/System.Diagnostics/Switch/Overview/source.cs index 96ce4f3f386..62e45943cd2 100644 --- a/snippets/csharp/System.Diagnostics/Switch/Overview/source.cs +++ b/snippets/csharp/System.Diagnostics/Switch/Overview/source.cs @@ -87,9 +87,9 @@ public class Class1 { /* Create an instance of MyMethodTracingSwitch.*/ static MyMethodTracingSwitch mySwitch = - new MyMethodTracingSwitch("Methods", "Trace entering and exiting method"); + new("Methods", "Trace entering and exiting method"); - public static void Main() + public static void Run() { // Add the console listener to see trace messages as console output Trace.Listeners.Add(new ConsoleTraceListener(true)); diff --git a/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/program.cs b/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/program.cs index 4d05b0ba34c..2f0715553c7 100644 --- a/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/program.cs +++ b/snippets/csharp/System.Diagnostics/SwitchAttribute/Overview/program.cs @@ -26,9 +26,8 @@ using System; using System.Collections; using System.Diagnostics; -using System.Reflection; using System.IO; -using System.Security.Permissions; +using System.Reflection; namespace Testing { @@ -36,7 +35,7 @@ class TraceTest { // // Initialize the trace source. - static TraceSource ts = new TraceSource("TraceTest"); + static TraceSource ts = new("TraceTest"); // // [SwitchAttribute("SourceSwitch", typeof(SourceSwitch))] @@ -48,7 +47,7 @@ static void Main() // Initialize trace switches. // #if(!ConfigFile) - SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose"); + SourceSwitch sourceSwitch = new("SourceSwitch", "Verbose"); ts.Switch = sourceSwitch; int idxConsole = ts.Listeners.Add(new System.Diagnostics.ConsoleTraceListener()); ts.Listeners[idxConsole].Name = "console"; @@ -92,7 +91,7 @@ static void Main() ts.TraceData(TraceEventType.Warning, 7, new object()); // // - ts.TraceData(TraceEventType.Warning, 8, new object[] { "Message 1", "Message 2" }); + ts.TraceData(TraceEventType.Warning, 8, ["Message 1", "Message 2"]); // // Activity tests. // @@ -111,7 +110,7 @@ static void Main() catch (Exception e) { // Catch any unexpected exception. - Console.WriteLine("Unexpected exception: " + e.ToString()); + Console.WriteLine($"Unexpected exception: {e}"); Console.Read(); } } diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Program.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Program.cs new file mode 100644 index 00000000000..da5f8f180a0 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Program.cs @@ -0,0 +1,32 @@ +using System; + +string[] sampleArgs = args.Length > 1 ? args[1..] : []; + +switch (args.Length > 0 ? args[0] : null) +{ + case "basic": + TextWriterTraceListenerSample.Run(); + break; + case "writer": + Sample.Run(sampleArgs); + break; + case "stream": + TWTLConStreamMod.Run(sampleArgs); + break; + case "named-stream": + TWTLConStreamNameMod.Run(sampleArgs); + break; + case "file": + TWTLConStringMod.Run(sampleArgs); + break; + case "named-file": + TWTLConStringNameMod.Run(sampleArgs); + break; + case "named-writer": + TWTLConWriterNameMod.Run(sampleArgs); + break; + default: + Console.WriteLine( + "Specify: basic, writer, stream, named-stream, file, named-file, or named-writer."); + break; +} diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Project.csproj b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Project.csproj index e0fb3c9027d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/Project.csproj @@ -1,9 +1,6 @@ - Exe net10.0 - TextWriterTraceListenerSample - diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source.cs index 4dafedc1818..a9ed2fe5f65 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source.cs @@ -1,31 +1,29 @@ -// +// #define TRACE using System; -using System.IO; using System.Diagnostics; +using System.IO; public class TextWriterTraceListenerSample { - public static void Main() + public static void Run() { - TextWriterTraceListener myTextListener = null; - // Create a file for output named TestFile.txt. string myFileName = "TestFile.txt"; - StreamWriter myOutputWriter = new StreamWriter(myFileName, true); + using StreamWriter myOutputWriter = new(myFileName, true); // Add a TextWriterTraceListener for the file. - myTextListener = new TextWriterTraceListener(myOutputWriter); + using TextWriterTraceListener myTextListener = + new TextWriterTraceListener(myOutputWriter); Trace.Listeners.Add(myTextListener); // Write trace output to all trace listeners. - Trace.WriteLine(DateTime.Now.ToString() + " - Trace output"); + Trace.WriteLine($"{DateTime.Now} - Trace output"); // Remove and close the file writer/trace listener. myTextListener.Flush(); Trace.Listeners.Remove(myTextListener); - myTextListener.Close(); } } // diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source1.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source1.cs index 813b60e9845..0463c30cca7 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source1.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/source1.cs @@ -4,12 +4,14 @@ // public class Sample { - public static void Main(string[] args) + public static void Run(string[] args) { /* Create a text writer that writes to the console screen and add * it to the trace listeners */ - TextWriterTraceListener myWriter = new TextWriterTraceListener(); - myWriter.Writer = System.Console.Out; + TextWriterTraceListener myWriter = new TextWriterTraceListener() + { + Writer = System.Console.Out + }; Trace.Listeners.Add(myWriter); // Write the output to the console screen. diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstream.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstream.cs index c8516c278bc..5c492a20b16 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstream.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstream.cs @@ -2,17 +2,16 @@ using System; using System.Diagnostics; using System.IO; -using Microsoft.VisualBasic; class TWTLConStreamMod { // args(0) is the specification of the trace log file. - public static void Main(string[] args) + public static void Run(string[] args) { // Verify that a parameter was entered. - if (args.Length==0) + if (args.Length == 0) { Console.WriteLine("Enter a trace file specification."); } @@ -24,7 +23,7 @@ public static void Main(string[] args) { traceStream = new FileStream(args[0], FileMode.Append, FileAccess.Write); } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine("Error creating FileStream for trace file \"{0}\":" + "\r\n{1}", args[0], ex.Message); @@ -37,12 +36,12 @@ public static void Main(string[] args) Trace.Listeners.Add(textListener); // Write these messages only to this TextWriterTraceListener. - textListener.WriteLine("This is trace listener named \""+ textListener.Name+"\""); + textListener.WriteLine("This is trace listener named \"" + textListener.Name + "\""); textListener.WriteLine("Trace written through a stream to: " + - "\r\n \""+args[0]+"\""); + "\r\n \"" + args[0] + "\""); // Write a message to all trace listeners. - Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now)); + Trace.WriteLine($"This trace message written {DateTime.Now} to all listeners."); // Flush and close the output. Trace.Flush(); diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstreamname.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstreamname.cs index 31f6882918f..d23a9c2770d 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstreamname.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstreamname.cs @@ -2,19 +2,18 @@ using System; using System.Diagnostics; using System.IO; -using Microsoft.VisualBasic; class TWTLConStreamNameMod { - const string LISTENER_NAME = "myStreamListener"; + const string ListenerName = "myStreamListener"; // args(0) is the specification of the trace log file. - public static void Main(string[] args) + public static void Run(string[] args) { // Verify that a parameter was entered. - if (args.Length==0) + if (args.Length == 0) { Console.WriteLine("Enter a trace file specification."); } @@ -26,7 +25,7 @@ public static void Main(string[] args) { traceStream = new FileStream(args[0], FileMode.Append, FileAccess.Write); } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine("Error creating FileStream for trace file \"{0}\":" + "\r\n{1}", args[0], ex.Message); @@ -35,16 +34,16 @@ public static void Main(string[] args) // Create a TextWriterTraceListener object that takes a stream. TextWriterTraceListener textListener; - textListener = new TextWriterTraceListener(traceStream, LISTENER_NAME); + textListener = new TextWriterTraceListener(traceStream, ListenerName); Trace.Listeners.Add(textListener); // Write these messages only to the TextWriterTraceListener. - textListener.WriteLine("This is trace listener named \""+textListener.Name+"\""); + textListener.WriteLine("This is trace listener named \"" + textListener.Name + "\""); textListener.WriteLine("Trace written through a stream to: " + - "\r\n \""+args[0]+"\""); + "\r\n \"" + args[0] + "\""); // Write a message to all trace listeners. - Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now)); + Trace.WriteLine($"This trace message written {DateTime.Now} to all listeners."); // Flush and close the output. Trace.Flush(); diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstring.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstring.cs index c592f2226d4..827cc553140 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstring.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstring.cs @@ -1,17 +1,16 @@ // using System; using System.Diagnostics; -using Microsoft.VisualBasic; class TWTLConStringMod { // args(0) is the specification of the trace log file. - public static void Main(string[] args) + public static void Run(string[] args) { // Verify that a parameter was entered. - if (args.Length==0) + if (args.Length == 0) { Console.WriteLine("Enter a trace file specification."); } @@ -25,7 +24,7 @@ public static void Main(string[] args) textListener = new TextWriterTraceListener(args[0]); Trace.Listeners.Add(textListener); } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine("Error creating TextWriterTraceListener for trace " + "file \"{0}\":\r\n{1}", args[0], ex.Message); @@ -33,12 +32,12 @@ public static void Main(string[] args) } // Write these messages only to the TextWriterTraceListener. - textListener.WriteLine("This is trace listener named \""+textListener.Name+"\""); + textListener.WriteLine("This is trace listener named \"" + textListener.Name + "\""); textListener.WriteLine("Trace written to a file: " + - "\r\n \""+args[0]+"\""); + "\r\n \"" + args[0] + "\""); // Write a message to all trace listeners. - Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now)); + Trace.WriteLine($"This trace message written {DateTime.Now} to all listeners."); // Flush and close the output. Trace.Flush(); diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstringname.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstringname.cs index f2bcea96b64..0b3815685b4 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstringname.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconstringname.cs @@ -1,19 +1,18 @@ // using System; using System.Diagnostics; -using Microsoft.VisualBasic; class TWTLConStringNameMod { - const string LISTENER_NAME = "myStringListener"; + const string ListenerName = "myStringListener"; // args(0) is the specification of the trace log file. - public static void Main(string[] args) + public static void Run(string[] args) { // Verify that a parameter was entered. - if (args.Length==0) + if (args.Length == 0) { Console.WriteLine("Enter a trace file specification."); } @@ -24,10 +23,10 @@ public static void Main(string[] args) TextWriterTraceListener textListener; try { - textListener = new TextWriterTraceListener(args[0], LISTENER_NAME); + textListener = new TextWriterTraceListener(args[0], ListenerName); Trace.Listeners.Add(textListener); } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine("Error creating TextWriterTraceListener for trace " + "file \"{0}\":\r\n{1}", args[0], ex.Message); @@ -35,12 +34,12 @@ public static void Main(string[] args) } // Write these messages only to this TextWriterTraceListener. - textListener.WriteLine("This is trace listener named \""+textListener.Name+"\""); + textListener.WriteLine("This is trace listener named \"" + textListener.Name + "\""); textListener.WriteLine("Trace written to a file: " + - "\r\n \""+args[0]+"\""); + "\r\n \"" + args[0] + "\""); // Write a message to all trace listeners. - Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now)); + Trace.WriteLine($"This trace message written {DateTime.Now} to all listeners."); // Flush and close the output. Trace.Flush(); diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconwritername.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconwritername.cs index c14a05a2f39..c27f461cae7 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconwritername.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/.ctor/twtlconwritername.cs @@ -2,19 +2,18 @@ using System; using System.Diagnostics; using System.IO; -using Microsoft.VisualBasic; class TWTLConWriterNameMod { - const string LISTENER_NAME = "myWriterListener"; + const string ListenerName = "myWriterListener"; // args(0) is the specification of the trace log file. - public static void Main(string[] args) + public static void Run(string[] args) { // Verify that a parameter was entered. - if (args.Length==0) + if (args.Length == 0) { Console.WriteLine("Enter a trace file specification."); } @@ -26,7 +25,7 @@ public static void Main(string[] args) { traceWriter = new StreamWriter(args[0], true); } - catch(Exception ex) + catch (Exception ex) { Console.WriteLine("Error creating StreamWriter for trace file \"{0}\":" + "\r\n{1}", args[0], ex.Message); @@ -35,16 +34,16 @@ public static void Main(string[] args) // Create a TextWriterTraceListener that takes a StreamWriter. TextWriterTraceListener textListener; - textListener = new TextWriterTraceListener(traceWriter, LISTENER_NAME); + textListener = new TextWriterTraceListener(traceWriter, ListenerName); Trace.Listeners.Add(textListener); // Write these messages only to this TextWriterTraceListener. - textListener.WriteLine("This is trace listener named \""+textListener.Name+"\""); + textListener.WriteLine("This is trace listener named \"" + textListener.Name + "\""); textListener.WriteLine("Trace written through a stream to: " + - "\r\n \""+args[0]+"\""); + "\r\n \"" + args[0] + "\""); // Write a message to all trace listeners. - Trace.WriteLine(String.Format("This trace message written {0} to all listeners.", DateTime.Now)); + Trace.WriteLine($"This trace message written {DateTime.Now} to all listeners."); // Flush and close the output. Trace.Flush(); diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/source.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/source.cs index 842407a72f5..9db987993cf 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/source.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Overview/source.cs @@ -1,28 +1,29 @@ using System; -using System.IO; using System.Diagnostics; +using System.IO; // public class Sample { -public static int Main(string[] args) { - // Create a file for output named TestFile.txt. - Stream myFile = File.Create("TestFile.txt"); + public static int Main(string[] args) + { + // Create a file for output named TestFile.txt. + using Stream myFile = File.Create("TestFile.txt"); - /* Create a new text writer using the output stream, and add it to - * the trace listeners. */ - TextWriterTraceListener myTextListener = new - TextWriterTraceListener(myFile); - Trace.Listeners.Add(myTextListener); + /* Create a new text writer using the output stream, and add it to + * the trace listeners. */ + using TextWriterTraceListener myTextListener = new(myFile); + Trace.Listeners.Add(myTextListener); - // Write output to the file. - Trace.Write("Test output "); + // Write output to the file. + Trace.Write("Test output "); - // Flush the output. - Trace.Flush(); + // Flush the output. + Trace.Flush(); + Trace.Listeners.Remove(myTextListener); - return 0; - } + return 0; + } } // diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/Project.csproj b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/Project.csproj index a369cfa8a80..1eddf5e00ac 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/Project.csproj @@ -1,8 +1,6 @@ - Library net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/source.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/source.cs index 61ef2258369..b2406bb1b48 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/source.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Write/source.cs @@ -5,20 +5,23 @@ public class Sample { - void Main(string[] args) { - /* Create a text writer that writes to the console screen and add - * it to the trace listeners */ - TextWriterTraceListener myWriter = new TextWriterTraceListener(); - myWriter.Writer = System.Console.Out; - Trace.Listeners.Add(myWriter); + void Main(string[] args) + { + /* Create a text writer that writes to the console screen and add + * it to the trace listeners */ + TextWriterTraceListener myWriter = new() + { + Writer = System.Console.Out + }; + Trace.Listeners.Add(myWriter); - // Write the output to the console screen. - myWriter.Write("Write to console screen. "); - myWriter.WriteLine("Again, write to the Console screen."); + // Write the output to the console screen. + myWriter.Write("Write to console screen. "); + myWriter.WriteLine("Again, write to the Console screen."); - // Flush and close the output. - myWriter.Flush(); - myWriter.Close(); - } + // Flush and close the output. + myWriter.Flush(); + myWriter.Close(); + } } // diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/Project.csproj b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/Project.csproj index a369cfa8a80..1eddf5e00ac 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/Project.csproj @@ -1,8 +1,6 @@ - Library net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/source.cs b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/source.cs index 951a78a6800..315137ce9e8 100644 --- a/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/source.cs +++ b/snippets/csharp/System.Diagnostics/TextWriterTraceListener/Writer/source.cs @@ -3,14 +3,16 @@ public class Sample { - protected void Method() - { -// + protected void Method() + { + // - TextWriterTraceListener myWriter = new TextWriterTraceListener(); - myWriter.Writer = System.Console.Out; - Trace.Listeners.Add(myWriter); + TextWriterTraceListener myWriter = new() + { + Writer = System.Console.Out + }; + Trace.Listeners.Add(myWriter); -// - } + // + } } diff --git a/snippets/csharp/System.Diagnostics/Trace/Assert/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/Assert/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Assert/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Trace/Assert/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Trace/Assert/source.cs b/snippets/csharp/System.Diagnostics/Trace/Assert/source.cs index cb4aa3cb99a..701dfef9881 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Assert/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Assert/source.cs @@ -1,8 +1,9 @@ -using System.Diagnostics; +using System; +using System.Diagnostics; // // Create an index for an array. -int index; +int index = default; void Method() { @@ -12,3 +13,5 @@ void Method() Trace.Assert(index > -1); } // + +_ = (Action)Method; diff --git a/snippets/csharp/System.Diagnostics/Trace/Assert/source1.cs b/snippets/csharp/System.Diagnostics/Trace/Assert/source1.cs index 3225ebec17b..72b737d1974 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Assert/source1.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Assert/source1.cs @@ -6,7 +6,7 @@ public class Form1 // public static void MyMethod(Type type, Type baseType) { - Trace.Assert(type != null, "Type parameter is null"); + Trace.Assert(type is not null, "Type parameter is null"); // Perform some processing. } diff --git a/snippets/csharp/System.Diagnostics/Trace/Assert/source2.cs b/snippets/csharp/System.Diagnostics/Trace/Assert/source2.cs index 407e7ba88b1..7d06ff4459f 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Assert/source2.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Assert/source2.cs @@ -6,7 +6,7 @@ public class Form2 // public static void MyMethod(Type type, Type baseType) { - Trace.Assert(type != null, "Type parameter is null", + Trace.Assert(type is not null, "Type parameter is null", "Can't get object for null type"); // Perform some processing. diff --git a/snippets/csharp/System.Diagnostics/Trace/Close/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/Close/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Close/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Trace/Close/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Trace/Close/source.cs b/snippets/csharp/System.Diagnostics/Trace/Close/source.cs index 3d048e3b370..91112f5bae8 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Close/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Close/source.cs @@ -2,21 +2,21 @@ // Specify /d:TRACE when compiling. using System; -using System.IO; using System.Diagnostics; +using System.IO; class Test { static void Main() { // Create a file for output named TestFile.txt. - using (FileStream myFileStream = - new FileStream("TestFile.txt", FileMode.Append)) + using FileStream myFileStream = + new("TestFile.txt", FileMode.Append); { // Create a new text writer using the output stream // and add it to the trace listeners. TextWriterTraceListener myTextListener = - new TextWriterTraceListener(myFileStream); + new(myFileStream); Trace.Listeners.Add(myTextListener); // Write output to the file. diff --git a/snippets/csharp/System.Diagnostics/Trace/Fail/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/Fail/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Fail/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Trace/Fail/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Trace/Fail/source.cs b/snippets/csharp/System.Diagnostics/Trace/Fail/source.cs index 4e3ed0c5cca..2eda60b27be 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Fail/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Fail/source.cs @@ -1,22 +1,25 @@ using System; -using System.Data; using System.Diagnostics; public class Form1 { - public enum Option { First, Second }; + public enum Option + { + First, + Second + } protected double result; public void Method(Option option) { try { - // try something here + // Try something here. } // catch (Exception) { - Trace.Fail("Unknown Option " + option + ", using the default."); + Trace.Fail($"Unknown Option {option}, using the default."); } // // @@ -28,7 +31,7 @@ public void Method(Option option) // Insert additional cases. default: - Trace.Fail("Unknown Option " + option); + Trace.Fail($"Unknown Option {option}"); result = 1.0; break; } diff --git a/snippets/csharp/System.Diagnostics/Trace/Fail/source1.cs b/snippets/csharp/System.Diagnostics/Trace/Fail/source1.cs index 94ea74479dd..0f2728b3155 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Fail/source1.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Fail/source1.cs @@ -1,12 +1,15 @@ #define TRACE using System; -using System.Data; using System.Diagnostics; public class Form2 { - public enum Option { First, Second }; + public enum Option + { + First, + Second + } protected double result; @@ -21,7 +24,7 @@ public void Method(Option option, string userInput) // catch (Exception) { - Trace.Fail("Invalid value: " + value.ToString(), + Trace.Fail($"Invalid value: {value}", "Resetting value to newValue."); value = newValue; } @@ -36,7 +39,7 @@ public void Method(Option option, string userInput) // Insert additional cases. default: - Trace.Fail("Unsupported option " + option, "Result set to 1.0"); + Trace.Fail($"Unsupported option {option}", "Result set to 1.0"); result = 1.0; break; } @@ -46,7 +49,7 @@ public void Method(Option option, string userInput) [STAThread] static void Main() { - var myForm = new Form2(); + Form2 myForm = new(); myForm.Method(Option.Second, "not an integer string"); } } diff --git a/snippets/csharp/System.Diagnostics/Trace/Indent/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/Indent/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Indent/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Trace/Indent/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Trace/Indent/source.cs b/snippets/csharp/System.Diagnostics/Trace/Indent/source.cs index a3e7bab4e04..a1cc67be887 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Indent/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Indent/source.cs @@ -1,6 +1,4 @@ -using System; -using System.Data; -using System.Diagnostics; +using System.Diagnostics; // Trace.WriteLine("List of errors:"); diff --git a/snippets/csharp/System.Diagnostics/Trace/Listeners/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/Listeners/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Listeners/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Trace/Listeners/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Trace/Listeners/source.cs b/snippets/csharp/System.Diagnostics/Trace/Listeners/source.cs index 36efa73b35e..ad55688e97c 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Listeners/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Listeners/source.cs @@ -1,10 +1,8 @@ -using System; -using System.Data; -using System.Diagnostics; +using System.Diagnostics; // /* Create a ConsoleTraceListener and add it to the trace listeners. */ -var myWriter = new ConsoleTraceListener(); +ConsoleTraceListener myWriter = new(); Trace.Listeners.Add(myWriter); // diff --git a/snippets/csharp/System.Diagnostics/Trace/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/Overview/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/Trace/Overview/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/Trace/Overview/source.cs b/snippets/csharp/System.Diagnostics/Trace/Overview/source.cs index e92b15d5441..250d977baea 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Overview/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Overview/source.cs @@ -8,13 +8,13 @@ class Test { static void Main() { - Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); - Trace.AutoFlush = true; - Trace.Indent(); - Trace.WriteLine("Entering Main"); - Console.WriteLine("Hello World."); - Trace.WriteLine("Exiting Main"); - Trace.Unindent(); + Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); + Trace.AutoFlush = true; + Trace.Indent(); + Trace.WriteLine("Entering Main"); + Console.WriteLine("Hello World."); + Trace.WriteLine("Exiting Main"); + Trace.Unindent(); } } // diff --git a/snippets/csharp/System.Diagnostics/Trace/Write/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/Write/Project.csproj new file mode 100644 index 00000000000..1eddf5e00ac --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Trace/Write/Project.csproj @@ -0,0 +1,6 @@ + + + Library + net10.0 + + diff --git a/snippets/csharp/System.Diagnostics/Trace/Write/source.cs b/snippets/csharp/System.Diagnostics/Trace/Write/source.cs index e55d857610e..76fb326d748 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Write/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Write/source.cs @@ -1,24 +1,22 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteSampleBase { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod() { - // Write the message if the TraceSwitch level is set to Error or higher. - if(generalSwitch.TraceError) - Trace.Write("My error message. "); + public static void MyErrorMethod() + { + // Write the message if the TraceSwitch level is set to Error or higher. + if (s_generalSwitch.TraceError) + Trace.Write("My error message. "); - // Write a second message if the TraceSwitch level is set to Verbose. - if(generalSwitch.TraceVerbose) - Trace.WriteLine("My second error message."); - } + // Write a second message if the TraceSwitch level is set to Verbose. + if (s_generalSwitch.TraceVerbose) + Trace.WriteLine("My second error message."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/Write/source1.cs b/snippets/csharp/System.Diagnostics/Trace/Write/source1.cs index e1f3d94e784..9ee6d7e62f7 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Write/source1.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Write/source1.cs @@ -1,24 +1,22 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteSample1 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject) { - // Write the message if the TraceSwitch level is set to Error or higher. - if(generalSwitch.TraceError) - Trace.Write(myObject); + public static void MyErrorMethod(object myObject) + { + // Write the message if the TraceSwitch level is set to Error or higher. + if (s_generalSwitch.TraceError) + Trace.Write(myObject); - // Write a second message if the TraceSwitch level is set to Verbose. - if(generalSwitch.TraceVerbose) - Trace.WriteLine(" is not a valid value for this method."); - } + // Write a second message if the TraceSwitch level is set to Verbose. + if (s_generalSwitch.TraceVerbose) + Trace.WriteLine(" is not a valid value for this method."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/Write/source2.cs b/snippets/csharp/System.Diagnostics/Trace/Write/source2.cs index 3a536963b4f..d93ef34ea9d 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Write/source2.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Write/source2.cs @@ -1,25 +1,22 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteSample2 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject, string category) { - // Write the message if the TraceSwitch level is set to Verbose. - if(generalSwitch.TraceVerbose) - Trace.Write(myObject.ToString() + " is not a valid object for category: ", - category); + public static void MyErrorMethod(object myObject, string category) + { + // Write the message if the TraceSwitch level is set to Verbose. + if (s_generalSwitch.TraceVerbose) + Trace.Write($"{myObject.ToString()} is not a valid object for category: ", category); - // Write a second message if the TraceSwitch level is set to Error or higher. - if(generalSwitch.TraceError) - Trace.WriteLine(" Please use a different category."); - } + // Write a second message if the TraceSwitch level is set to Error or higher. + if (s_generalSwitch.TraceError) + Trace.WriteLine(" Please use a different category."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/Write/source3.cs b/snippets/csharp/System.Diagnostics/Trace/Write/source3.cs index b0d556aa15e..73774252fba 100644 --- a/snippets/csharp/System.Diagnostics/Trace/Write/source3.cs +++ b/snippets/csharp/System.Diagnostics/Trace/Write/source3.cs @@ -1,24 +1,22 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteSample3 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject, string category) { - // Write the message if the TraceSwitch level is set to Verbose. - if(generalSwitch.TraceVerbose) - Trace.Write(myObject, category); + public static void MyErrorMethod(object myObject, string category) + { + // Write the message if the TraceSwitch level is set to Verbose. + if (s_generalSwitch.TraceVerbose) + Trace.Write(myObject, category); - // Write a second message if the TraceSwitch level is set to Error or higher. - if(generalSwitch.TraceError) - Trace.WriteLine(" Object is not valid for this category."); - } + // Write a second message if the TraceSwitch level is set to Error or higher. + if (s_generalSwitch.TraceError) + Trace.WriteLine(" Object is not valid for this category."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteIf/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/WriteIf/Project.csproj new file mode 100644 index 00000000000..1eddf5e00ac --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Trace/WriteIf/Project.csproj @@ -0,0 +1,6 @@ + + + Library + net10.0 + + diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source.cs b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source.cs index 3df3ad5ef02..ebb13d3626b 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source.cs @@ -1,22 +1,20 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteIfSampleBase { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod() { - // Write the message if the TraceSwitch level is set to Error or higher. - Trace.WriteIf(generalSwitch.TraceError, "My error message. "); + public static void MyErrorMethod() + { + // Write the message if the TraceSwitch level is set to Error or higher. + Trace.WriteIf(s_generalSwitch.TraceError, "My error message. "); - // Write a second message if the TraceSwitch level is set to Verbose. - Trace.WriteLineIf(generalSwitch.TraceVerbose, "My second error message."); - } + // Write a second message if the TraceSwitch level is set to Verbose. + Trace.WriteLineIf(s_generalSwitch.TraceVerbose, "My second error message."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source1.cs b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source1.cs index e74b3929dd4..91f083f240e 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source1.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source1.cs @@ -1,22 +1,20 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteIfSample1 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject) { - // Write the message if the TraceSwitch level is set to Error or higher. - Trace.WriteIf(generalSwitch.TraceError, myObject); + public static void MyErrorMethod(object myObject) + { + // Write the message if the TraceSwitch level is set to Error or higher. + Trace.WriteIf(s_generalSwitch.TraceError, myObject); - // Write a second message if the TraceSwitch level is set to Verbose. - Trace.WriteLineIf(generalSwitch.TraceVerbose, " is not a valid value for this method."); - } + // Write a second message if the TraceSwitch level is set to Verbose. + Trace.WriteLineIf(s_generalSwitch.TraceVerbose, " is not a valid value for this method."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source2.cs b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source2.cs index a1b70c73de9..32d16923f9a 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source2.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source2.cs @@ -1,23 +1,21 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteIfSample2 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject, string category) { - // Write the message if the TraceSwitch level is set to Verbose. - Trace.WriteIf(generalSwitch.TraceVerbose, myObject.ToString() + - " is not a valid object for category: ", category); + public static void MyErrorMethod(object myObject, string category) + { + // Write the message if the TraceSwitch level is set to Verbose. + Trace.WriteIf(s_generalSwitch.TraceVerbose, + $"{myObject.ToString()} is not a valid object for category: ", category); - // Write a second message if the TraceSwitch level is set to Error or higher. - Trace.WriteLineIf(generalSwitch.TraceError, " Please use a different category."); - } + // Write a second message if the TraceSwitch level is set to Error or higher. + Trace.WriteLineIf(s_generalSwitch.TraceError, " Please use a different category."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source3.cs b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source3.cs index bb972503467..8ad31b6563e 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteIf/source3.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteIf/source3.cs @@ -1,22 +1,20 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteIfSample3 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject, string category) { - // Write the message if the TraceSwitch level is set to Verbose. - Trace.WriteIf(generalSwitch.TraceVerbose, myObject, category); + public static void MyErrorMethod(object myObject, string category) + { + // Write the message if the TraceSwitch level is set to Verbose. + Trace.WriteIf(s_generalSwitch.TraceVerbose, myObject, category); - // Write a second message if the TraceSwitch level is set to Error or higher. - Trace.WriteLineIf(generalSwitch.TraceError, " Object is not valid for this category."); - } + // Write a second message if the TraceSwitch level is set to Error or higher. + Trace.WriteLineIf(s_generalSwitch.TraceError, " Object is not valid for this category."); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLine/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/WriteLine/Project.csproj new file mode 100644 index 00000000000..1eddf5e00ac --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLine/Project.csproj @@ -0,0 +1,6 @@ + + + Library + net10.0 + + diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLine/source.cs b/snippets/csharp/System.Diagnostics/Trace/WriteLine/source.cs index c14e79edf2b..0e278d1dbc7 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteLine/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLine/source.cs @@ -1,24 +1,22 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteLineSampleBase { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject) { - // Write the message if the TraceSwitch level is set to Error or higher. - if(generalSwitch.TraceError) - Trace.Write("Invalid object. "); + public static void MyErrorMethod(object myObject) + { + // Write the message if the TraceSwitch level is set to Error or higher. + if (s_generalSwitch.TraceError) + Trace.Write("Invalid object. "); - // Write a second message if the TraceSwitch level is set to Verbose. - if(generalSwitch.TraceVerbose) - Trace.WriteLine(myObject); - } + // Write a second message if the TraceSwitch level is set to Verbose. + if (s_generalSwitch.TraceVerbose) + Trace.WriteLine(myObject); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLine/source1.cs b/snippets/csharp/System.Diagnostics/Trace/WriteLine/source1.cs index 74864ed46a0..6e6e5409b63 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteLine/source1.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLine/source1.cs @@ -1,24 +1,22 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteLineSample1 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(string category) { - // Write the message if the TraceSwitch level is set to Error or higher. - if(generalSwitch.TraceError) - Trace.Write("My error message. "); + public static void MyErrorMethod(string category) + { + // Write the message if the TraceSwitch level is set to Error or higher. + if (s_generalSwitch.TraceError) + Trace.Write("My error message. "); - // Write a second message if the TraceSwitch level is set to Verbose. - if(generalSwitch.TraceVerbose) - Trace.WriteLine("My second error message.", category); - } + // Write a second message if the TraceSwitch level is set to Verbose. + if (s_generalSwitch.TraceVerbose) + Trace.WriteLine("My second error message.", category); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLine/source2.cs b/snippets/csharp/System.Diagnostics/Trace/WriteLine/source2.cs index a0519146199..9a73b10a3d0 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteLine/source2.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLine/source2.cs @@ -1,24 +1,22 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteLineSample2 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject, string category) { - // Write the message if the TraceSwitch level is set to Error or higher. - if(generalSwitch.TraceError) - Trace.Write("Invalid object for category. "); + public static void MyErrorMethod(object myObject, string category) + { + // Write the message if the TraceSwitch level is set to Error or higher. + if (s_generalSwitch.TraceError) + Trace.Write("Invalid object for category. "); - // Write a second message if the TraceSwitch level is set to Verbose. - if(generalSwitch.TraceVerbose) - Trace.WriteLine(myObject, category); - } + // Write a second message if the TraceSwitch level is set to Verbose. + if (s_generalSwitch.TraceVerbose) + Trace.WriteLine(myObject, category); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/Project.csproj b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/Project.csproj new file mode 100644 index 00000000000..1eddf5e00ac --- /dev/null +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/Project.csproj @@ -0,0 +1,6 @@ + + + Library + net10.0 + + diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source.cs b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source.cs index d16db20fd12..cf17089990e 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source.cs @@ -1,22 +1,20 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteLineIfSampleBase { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject) { - // Write the message if the TraceSwitch level is set to Error or higher. - Trace.WriteIf(generalSwitch.TraceError, "Invalid object. "); + public static void MyErrorMethod(object myObject) + { + // Write the message if the TraceSwitch level is set to Error or higher. + Trace.WriteIf(s_generalSwitch.TraceError, "Invalid object. "); - // Write a second message if the TraceSwitch level is set to Verbose. - Trace.WriteLineIf(generalSwitch.TraceVerbose, myObject); - } + // Write a second message if the TraceSwitch level is set to Verbose. + Trace.WriteLineIf(s_generalSwitch.TraceVerbose, myObject); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source1.cs b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source1.cs index e227e8a93aa..4c1784dcc2a 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source1.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source1.cs @@ -1,22 +1,20 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteLineIfSample1 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(string category) { - // Write the message if the TraceSwitch level is set to Error or higher. - Trace.WriteIf(generalSwitch.TraceError, "My error message. "); + public static void MyErrorMethod(string category) + { + // Write the message if the TraceSwitch level is set to Error or higher. + Trace.WriteIf(s_generalSwitch.TraceError, "My error message. "); - // Write a second message if the TraceSwitch level is set to Verbose. - Trace.WriteLineIf(generalSwitch.TraceVerbose, "My second error message.", category); - } + // Write a second message if the TraceSwitch level is set to Verbose. + Trace.WriteLineIf(s_generalSwitch.TraceVerbose, "My second error message.", category); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source2.cs b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source2.cs index 01dbbf32058..853a4b38d0a 100644 --- a/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source2.cs +++ b/snippets/csharp/System.Diagnostics/Trace/WriteLineIf/source2.cs @@ -1,22 +1,20 @@ -using System; -using System.Data; -using System.Diagnostics; -using System.Windows.Forms; +using System.Diagnostics; -public class Form1: Form +public class WriteLineIfSample2 { -// -// Class-level declaration. - // Create a TraceSwitch. - static TraceSwitch generalSwitch = new TraceSwitch("General", "Entire Application"); + // + // Class-level declaration. + // Create a TraceSwitch. + private static readonly TraceSwitch s_generalSwitch = new("General", "Entire Application"); - static public void MyErrorMethod(Object myObject, string category) { - // Write the message if the TraceSwitch level is set to Error or higher. - Trace.WriteIf(generalSwitch.TraceError, "Invalid object for category. "); + public static void MyErrorMethod(object myObject, string category) + { + // Write the message if the TraceSwitch level is set to Error or higher. + Trace.WriteIf(s_generalSwitch.TraceError, "Invalid object for category. "); - // Write a second message if the TraceSwitch level is set to Verbose. - Trace.WriteLineIf(generalSwitch.TraceVerbose, myObject, category); - } + // Write a second message if the TraceSwitch level is set to Verbose. + Trace.WriteLineIf(s_generalSwitch.TraceVerbose, myObject, category); + } -// + // } diff --git a/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/Project.csproj b/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/source.cs b/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/source.cs index bb49d210838..a5c527ef6f0 100644 --- a/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/source.cs +++ b/snippets/csharp/System.Diagnostics/TraceFilter/ShouldTrace/source.cs @@ -10,13 +10,15 @@ class TraceTest { static void Main() { - TraceSource ts = new TraceSource("TraceTest"); - SourceSwitch sourceSwitch = new SourceSwitch("SourceSwitch", "Verbose"); + TraceSource ts = new("TraceTest"); + SourceSwitch sourceSwitch = new("SourceSwitch", "Verbose"); ts.Switch = sourceSwitch; - ConsoleTraceListener ctl = new ConsoleTraceListener(); - ctl.Name = "console"; - ctl.TraceOutputOptions = TraceOptions.DateTime; - ctl.Filter = new ErrorFilter(); + ConsoleTraceListener ctl = new() + { + Name = "console", + TraceOutputOptions = TraceOptions.DateTime, + Filter = new ErrorFilter() + }; ts.Listeners.Add(ctl); ts.TraceEvent(TraceEventType.Warning, 1, "*** This event will be filtered out ***"); diff --git a/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/Project.csproj b/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/program.cs b/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/program.cs index b20e24078be..8cf8a9c79db 100644 --- a/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/program.cs +++ b/snippets/csharp/System.Diagnostics/TraceListener/GetSupportedAttributes/program.cs @@ -22,17 +22,17 @@ // using System; -using System.Diagnostics; +using System.Collections; using System.Configuration; +using System.Diagnostics; using System.Reflection; -using System.Collections; namespace CustomTraceListener { class Program { static void Main(string[] args) { - TraceSource ts = new TraceSource("TraceTest"); + TraceSource ts = new("TraceTest"); Console.WriteLine(ts.Switch.DisplayName); foreach (TraceListener traceListener in ts.Listeners) { @@ -75,7 +75,7 @@ public override void WriteLine(string s) } protected override string[] GetSupportedAttributes() { - return new string[] { "Source" }; + return ["Source"]; } } } @@ -90,4 +90,4 @@ protected override string[] GetSupportedAttributes() null TestListener test: TraceTest Error: 1 : test error message */ -// \ No newline at end of file +// diff --git a/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/Project.csproj b/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/source.cs b/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/source.cs index b0b36a2447f..5787b56aa9d 100644 --- a/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/source.cs +++ b/snippets/csharp/System.Diagnostics/TraceListenerCollection/Add/source.cs @@ -1,12 +1,13 @@ using System; -using System.Data; using System.Diagnostics; // /* Create a listener, which outputs to the console screen, and * add it to the trace listeners. */ -TextWriterTraceListener myWriter = new TextWriterTraceListener(); -myWriter.Writer = System.Console.Out; +TextWriterTraceListener myWriter = new() +{ + Writer = System.Console.Out +}; Trace.Listeners.Add(myWriter); // diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/Level/Project.csproj b/snippets/csharp/System.Diagnostics/TraceSwitch/Level/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/Level/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/Level/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/Level/source.cs b/snippets/csharp/System.Diagnostics/TraceSwitch/Level/source.cs index 0a8275e51a1..eec197c1fab 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/Level/source.cs +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/Level/source.cs @@ -1,24 +1,27 @@ using System; -using System.Data; using System.Diagnostics; public class Form1 { // - //Class-level declaration. - /* Create a TraceSwitch to use in the entire application.*/ + // Class-level declaration. + /* Create a TraceSwitch to use in the entire application. */ - static TraceSwitch mySwitch = new TraceSwitch("mySwitch", "Entire Application"); + private static readonly TraceSwitch s_mySwitch = new("mySwitch", "Entire Application"); - static public void MyMethod() + public static void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. - if (mySwitch.TraceError) + if (s_mySwitch.TraceError) + { Console.WriteLine("My error message."); + } // Write the message if the TraceSwitch level is set to Verbose. - if (mySwitch.TraceVerbose) + if (s_mySwitch.TraceVerbose) + { Console.WriteLine("My second error message."); + } } public static void Main(string[] args) diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Program.cs b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Program.cs new file mode 100644 index 00000000000..6c48c11e804 --- /dev/null +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Program.cs @@ -0,0 +1,16 @@ +using System; + +string[] sampleArgs = args.Length > 1 ? args[1..] : []; + +switch (args.Length > 0 ? args[0] : null) +{ + case "remarks": + TraceErr.Run(sampleArgs); + break; + case "overview": + Form1.Run(sampleArgs); + break; + default: + Console.WriteLine("Specify: remarks or overview."); + break; +} diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Project.csproj index c94dabbf2a0..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/Project.csproj @@ -1,9 +1,6 @@ - Exe net10.0 - TraceErr - diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/remarks.cs b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/remarks.cs index 5de16814dd2..27a9fa41710 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/remarks.cs +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/remarks.cs @@ -4,16 +4,16 @@ public class TraceErr { -// - private static TraceSwitch appSwitch = new TraceSwitch("mySwitch", + // + private static readonly TraceSwitch s_appSwitch = new("mySwitch", "Switch in config file"); - public static void Main(string[] args) + public static void Run(string[] args) { //... Console.WriteLine("Trace switch {0} configured as {1}", - appSwitch.DisplayName, appSwitch.Level.ToString()); - if (appSwitch.TraceError) + s_appSwitch.DisplayName, s_appSwitch.Level); + if (s_appSwitch.TraceError) { //... } diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/source.cs b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/source.cs index 0a04fd6949e..3c23ea26e05 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/source.cs +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/Overview/source.cs @@ -1,26 +1,29 @@ using System; -using System.Data; using System.Diagnostics; public class Form1 { // - //Class-level declaration. - /* Create a TraceSwitch to use in the entire application.*/ - static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application"); + // Class-level declaration. + /* Create a TraceSwitch to use in the entire application. */ + private static readonly TraceSwitch s_mySwitch = new("General", "Entire Application"); - static public void MyMethod() + public static void MyMethod() { // Write the message if the TraceSwitch level is set to Error or higher. - if (mySwitch.TraceError) + if (s_mySwitch.TraceError) + { Console.WriteLine("My error message."); + } // Write the message if the TraceSwitch level is set to Verbose. - if (mySwitch.TraceVerbose) + if (s_mySwitch.TraceVerbose) + { Console.WriteLine("My second error message."); + } } - public static void Main(string[] args) + public static void Run(string[] args) { // Run the method that prints error messages based on the switch level. MyMethod(); diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/Project.csproj b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/source.cs b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/source.cs index 87580e3e083..55b020e5640 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/source.cs +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceInfo/source.cs @@ -1,23 +1,26 @@ using System; -using System.Data; using System.Diagnostics; public class Form1 { // - //Class-level declaration. - /* Create a TraceSwitch to use in the entire application.*/ - static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application"); + // Class-level declaration. + /* Create a TraceSwitch to use in the entire application. */ + private static readonly TraceSwitch s_mySwitch = new("General", "Entire Application"); - static public void MyMethod() + public static void MyMethod() { // Write the message if the TraceSwitch level is set to Info or higher. - if (mySwitch.TraceInfo) + if (s_mySwitch.TraceInfo) + { Console.WriteLine("My error message."); + } // Write the message if the TraceSwitch level is set to Verbose. - if (mySwitch.TraceVerbose) + if (s_mySwitch.TraceVerbose) + { Console.WriteLine("My second error message."); + } } public static void Main(string[] args) diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/Project.csproj b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/Project.csproj index ffb97e9872d..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/Project.csproj +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/Project.csproj @@ -1,8 +1,6 @@ - Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/source.cs b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/source.cs index 6c889c9ba92..ab0958237db 100644 --- a/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/source.cs +++ b/snippets/csharp/System.Diagnostics/TraceSwitch/TraceWarning/source.cs @@ -1,23 +1,26 @@ using System; -using System.Data; using System.Diagnostics; public class Form1 { // - //Class-level declaration. - /* Create a TraceSwitch to use in the entire application.*/ - static TraceSwitch mySwitch = new TraceSwitch("General", "Entire Application"); + // Class-level declaration. + /* Create a TraceSwitch to use in the entire application. */ + private static readonly TraceSwitch s_mySwitch = new("General", "Entire Application"); - static public void MyMethod() + public static void MyMethod() { // Write the message if the TraceSwitch level is set to Warning or higher. - if (mySwitch.TraceWarning) + if (s_mySwitch.TraceWarning) + { Console.WriteLine("My error message."); + } // Write the message if the TraceSwitch level is set to Verbose. - if (mySwitch.TraceVerbose) + if (s_mySwitch.TraceVerbose) + { Console.WriteLine("My second error message."); + } } public static void Main(string[] args) diff --git a/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/Project.csproj b/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/Project.csproj index a369cfa8a80..36a29620edb 100644 --- a/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/Project.csproj +++ b/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/Project.csproj @@ -1,8 +1,6 @@ - - Library + Exe net10.0 - - \ No newline at end of file + diff --git a/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/program.cs b/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/program.cs index 32e69a451cd..fd64c651f8d 100644 --- a/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/program.cs +++ b/snippets/csharp/System.Diagnostics/XmlWriterTraceListener/Overview/program.cs @@ -1,21 +1,21 @@ // using System; +using System.Diagnostics; using System.IO; using System.Xml; using System.Xml.XPath; -using System.Diagnostics; class testClass { static void Main() { File.Delete("NotEscaped.xml"); - TraceSource ts = new TraceSource("TestSource"); + TraceSource ts = new("TestSource"); ts.Listeners.Add(new XmlWriterTraceListener("NotEscaped.xml")); ts.Switch.Level = SourceLevels.All; string testString = "11"; - XmlTextReader myXml = new XmlTextReader(new StringReader(testString)); - XPathDocument xDoc = new XPathDocument(myXml); + XmlTextReader myXml = new(new StringReader(testString)); + XPathDocument xDoc = new(myXml); XPathNavigator myNav = xDoc.CreateNavigator(); ts.TraceData(TraceEventType.Error, 38, myNav); @@ -23,7 +23,7 @@ static void Main() ts.Close(); File.Delete("Escaped.xml"); - TraceSource ts2 = new TraceSource("TestSource2"); + TraceSource ts2 = new("TestSource2"); ts2.Listeners.Add(new XmlWriterTraceListener("Escaped.xml")); ts2.Switch.Level = SourceLevels.All; ts2.TraceData(TraceEventType.Error, 38, testString); @@ -32,4 +32,4 @@ static void Main() ts2.Close(); } } -// \ No newline at end of file +// diff --git a/xml/System.Diagnostics/Process.xml b/xml/System.Diagnostics/Process.xml index 7bb8fb247d3..67cc5a0d0f2 100644 --- a/xml/System.Diagnostics/Process.xml +++ b/xml/System.Diagnostics/Process.xml @@ -4568,7 +4568,7 @@ A thread in a process can migrate from processor to processor, with each migrati The last two examples in this section use the method to launch an executable named *Write500Lines.exe*. The following example contains its source code. -:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs"::: +:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/Write500Lines/write500lines.cs"::: :::code language="fsharp" source="~/snippets/fsharp/System.Diagnostics/Process/StandardOutput/write500lines.fs"::: :::code language="vb" source="~/snippets/visualbasic/api/system.diagnostics/process/standardoutput/write500lines.vb"::: @@ -4718,7 +4718,7 @@ You can use asynchronous read operations to avoid these dependencies and their d The last two examples in this section use the method to launch an executable named *Write500Lines.exe*. The following example contains its source code. -:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs"::: +:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/Write500Lines/write500lines.cs"::: :::code language="fsharp" source="~/snippets/fsharp/System.Diagnostics/Process/StandardOutput/write500lines.fs"::: :::code language="vb" source="~/snippets/visualbasic/api/system.diagnostics/process/standardoutput/write500lines.vb"::: diff --git a/xml/System.Diagnostics/ProcessStartInfo.xml b/xml/System.Diagnostics/ProcessStartInfo.xml index 4dad5f910ee..acf083feed7 100644 --- a/xml/System.Diagnostics/ProcessStartInfo.xml +++ b/xml/System.Diagnostics/ProcessStartInfo.xml @@ -377,7 +377,7 @@ If you use this property to set command-line arguments, method to launch an executable named *Write500Lines.exe*. The following example contains its source code. -:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs"::: +:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/Write500Lines/write500lines.cs"::: :::code language="vb" source="~/snippets/visualbasic/api/system.diagnostics/process/standardoutput/write500lines.vb"::: The following example shows how to read from a redirected error stream and wait for the child process to exit. It avoids a deadlock condition by calling `p.StandardError.ReadToEnd` before `p.WaitForExit`. A deadlock condition can result if the parent process calls `p.WaitForExit` before `p.StandardError.ReadToEnd` and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full stream. @@ -1189,7 +1189,7 @@ You can use asynchronous read operations to avoid these dependencies and their d The last two examples in this section use the method to launch an executable named *Write500Lines.exe*. The following example contains its source code. -:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/StandardOutput/write500lines.cs"::: +:::code language="csharp" source="~/snippets/csharp/System.Diagnostics/Process/Write500Lines/write500lines.cs"::: :::code language="vb" source="~/snippets/visualbasic/api/system.diagnostics/process/standardoutput/write500lines.vb"::: The following example shows how to read from a redirected stream and wait for the child process to exit. The example avoids a deadlock condition by calling `p.StandardOutput.ReadToEnd` before `p.WaitForExit`. A deadlock condition can result if the parent process calls `p.WaitForExit` before `p.StandardOutput.ReadToEnd` and the child process writes enough text to fill the redirected stream. The parent process would wait indefinitely for the child process to exit. The child process would wait indefinitely for the parent to read from the full stream. From ac69dc2863d25fb47493bfbd80a6a22eb0c4a140 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Tue, 1 Sep 2026 14:14:36 -0700 Subject: [PATCH 5/7] Fix broken links (#13035) --- _zip/missingapi.yml | 68 +++++++++---------- .../ObjectiveCMarshal+MessageSendFunction.xml | 4 +- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/_zip/missingapi.yml b/_zip/missingapi.yml index 812a09f8feb..76947e0e76a 100644 --- a/_zip/missingapi.yml +++ b/_zip/missingapi.yml @@ -38,139 +38,139 @@ references: - uid: OpenAI.Assistants.Assistant name: Assistant fullname: OpenAI.Assistants.Assistant - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Assistants.netstandard2.0.cs - uid: OpenAI.Assistants.AssistantClient name: AssistantClient fullname: OpenAI.Assistants.AssistantClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Assistants.netstandard2.0.cs - uid: OpenAI.Assistants.FunctionToolDefinition name: FunctionToolDefinition fullname: OpenAI.Assistants.FunctionToolDefinition - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Assistants.netstandard2.0.cs - uid: OpenAI.Audio.AudioClient name: AudioClient fullname: OpenAI.Audio.AudioClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Audio.netstandard2.0.cs - uid: OpenAI.Chat.ChatClient name: ChatClient fullname: OpenAI.Chat.ChatClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Chat.netstandard2.0.cs - uid: OpenAI.Chat.ChatCompletion name: ChatCompletion fullname: OpenAI.Chat.ChatCompletion - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Chat.netstandard2.0.cs - uid: OpenAI.Chat.ChatCompletionOptions name: ChatCompletionOptions fullname: OpenAI.Chat.ChatCompletionOptions - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Chat.netstandard2.0.cs - uid: OpenAI.Chat.ChatMessage name: ChatMessage fullname: OpenAI.Chat.ChatMessage - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Chat.netstandard2.0.cs - uid: OpenAI.Chat.ChatResponseFormat name: ChatResponseFormat fullname: OpenAI.Chat.ChatResponseFormat - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Chat.netstandard2.0.cs - uid: OpenAI.Chat.ChatTool name: ChatTool fullname: OpenAI.Chat.ChatTool - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Chat.netstandard2.0.cs - uid: OpenAI.Chat.StreamingChatCompletionUpdate name: StreamingChatCompletionUpdate fullname: OpenAI.Chat.StreamingChatCompletionUpdate - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Chat.netstandard2.0.cs - uid: OpenAI.Containers.ContainerClient name: ContainerClient fullname: OpenAI.Containers.ContainerClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Containers.netstandard2.0.cs - uid: OpenAI.Embeddings.EmbeddingClient name: EmbeddingClient fullname: OpenAI.Embeddings.EmbeddingClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Embeddings.netstandard2.0.cs - uid: OpenAI.Files.OpenAIFileClient name: OpenAIFileClient fullname: OpenAI.Files.OpenAIFileClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Files.netstandard2.0.cs - uid: OpenAI.Images.ImageClient name: ImageClient fullname: OpenAI.Images.ImageClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Images.netstandard2.0.cs - uid: OpenAI.OpenAIClient name: OpenAIClient fullname: OpenAI.OpenAIClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.netstandard2.0.cs - uid: OpenAI.Realtime.ConversationFunctionTool name: ConversationFunctionTool fullname: OpenAI.Realtime.ConversationFunctionTool - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Realtime.netstandard2.0.cs - uid: OpenAI.Realtime.RealtimeClient name: RealtimeClient fullname: OpenAI.Realtime.RealtimeClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Realtime.netstandard2.0.cs - uid: OpenAI.Realtime.RealtimeFunctionTool name: RealtimeFunctionTool fullname: OpenAI.Realtime.RealtimeFunctionTool - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Realtime.netstandard2.0.cs - uid: OpenAI.RealtimeConversation.ConversationFunctionTool name: ConversationFunctionTool fullname: OpenAI.RealtimeConversation.ConversationFunctionTool - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Realtime.netstandard2.0.cs - uid: OpenAI.RealtimeConversation.ConversationUpdate name: ConversationUpdate fullname: OpenAI.RealtimeConversation.ConversationUpdate - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.RealtimeConversation.netstandard2.0.cs - uid: OpenAI.RealtimeConversation.RealtimeConversationClient name: RealtimeConversationClient fullname: OpenAI.RealtimeConversation.RealtimeConversationClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.RealtimeConversation.netstandard2.0.cs - uid: OpenAI.RealtimeConversation.RealtimeConversationSession name: RealtimeConversationSession fullname: OpenAI.RealtimeConversation.RealtimeConversationSession - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.RealtimeConversation.netstandard2.0.cs - uid: OpenAI.Responses.CreateResponseOptions name: CreateResponseOptions fullname: OpenAI.Responses.CreateResponseOptions - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.FunctionTool name: FunctionTool fullname: OpenAI.Responses.FunctionTool - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.OpenAIResponse name: OpenAIResponse fullname: OpenAI.Responses.OpenAIResponse - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.OpenAIResponseClient name: OpenAIResponseClient fullname: OpenAI.Responses.OpenAIResponseClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.ResponseCreationOptions name: ResponseCreationOptions fullname: OpenAI.Responses.ResponseCreationOptions - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.ResponseItem name: ResponseItem fullname: OpenAI.Responses.ResponseItem - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.ResponseResult name: ResponseResult fullname: OpenAI.Responses.ResponseResult - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.ResponsesClient name: ResponsesClient fullname: OpenAI.Responses.ResponsesClient - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.ResponseTextFormat name: ResponseTextFormat fullname: OpenAI.Responses.ResponseTextFormat - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.ResponseTool name: ResponseTool fullname: OpenAI.Responses.ResponseTool - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: OpenAI.Responses.StreamingResponseUpdate name: StreamingResponseUpdate fullname: OpenAI.Responses.StreamingResponseUpdate - href: https://github.com/openai/openai-dotnet/blob/main/api/OpenAI.netstandard2.0.cs + href: https://github.com/openai/openai-dotnet/blob/main/api/released/netstandard2.0/OpenAI.Responses.netstandard2.0.cs - uid: Polly.CircuitBreaker.CircuitBreakerStrategyOptions`1 name: CircuitBreakerStrategyOptions`1 fullname: Polly.CircuitBreaker.CircuitBreakerStrategyOptions`1 diff --git a/xml/System.Runtime.InteropServices.ObjectiveC/ObjectiveCMarshal+MessageSendFunction.xml b/xml/System.Runtime.InteropServices.ObjectiveC/ObjectiveCMarshal+MessageSendFunction.xml index 6ed1a49c2bd..833e203e28c 100644 --- a/xml/System.Runtime.InteropServices.ObjectiveC/ObjectiveCMarshal+MessageSendFunction.xml +++ b/xml/System.Runtime.InteropServices.ObjectiveC/ObjectiveCMarshal+MessageSendFunction.xml @@ -88,7 +88,7 @@ 2 - Overrides the Objective-C runtime's objc_msgSend_stret(). + Overrides the Objective-C runtime's objc_msgSend_stret(). @@ -134,7 +134,7 @@ 4 - Overrides the Objective-C runtime's objc_msgSendSuper_stret(). + Overrides the Objective-C runtime's objc_msgSendSuper_stret(). From 3cc87528de5d6f25c1fa412afb8dfcb92aba6ac2 Mon Sep 17 00:00:00 2001 From: Genevieve Warren <24882762+gewarren@users.noreply.github.com> Date: Thu, 3 Sep 2026 13:24:39 -0700 Subject: [PATCH 6/7] Update SKILL.md with obsolete API replacement guidance (#13055) --- .github/skills/csharp-snippet-modernization/SKILL.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/skills/csharp-snippet-modernization/SKILL.md b/.github/skills/csharp-snippet-modernization/SKILL.md index 6c4c4f8b198..10bcf2120dc 100644 --- a/.github/skills/csharp-snippet-modernization/SKILL.md +++ b/.github/skills/csharp-snippet-modernization/SKILL.md @@ -34,7 +34,7 @@ Apply these changes when they preserve behavior and sample clarity: timeZoneTime.DateTime); ``` - Use raw string literals (or interpolated raw string literals) for paragraph-style output. -- Use target-typed `new` when the target type is evident. +- Use `new` to construct objects. Omit the type name when the target type is evident. - Use collection expressions to initialize collections and arrays wherever possible. - Convert eligible value-producing `switch` statements to switch expressions. - Use auto-implemented properties instead of defining a separate field. @@ -51,6 +51,8 @@ Apply these changes when they preserve behavior and sample clarity: - Remove `this.` where it's unnecessary. - Add `using` statements for disposable types, and remove redundant calls to `Close()` or `Dispose()`. - Prune unnecessary package references from project files. +- Use compound assignment. +- Replace calls to obsolete APIs with their recommended replacements (per the `ObsoleteAttribute` message and official docs). For example, replace `WebRequest` with `HttpClient`. Don't introduce `var`; this repository prefers explicit types. From 4a60ae28f2a7b606ced198befad41fb5c3441787 Mon Sep 17 00:00:00 2001 From: Krzysztof Wicher Date: Thu, 3 Sep 2026 22:40:32 +0200 Subject: [PATCH 7/7] SignedXml: drop .NET 12 InvalidOperationException documentation (#13054) The dotnet/runtime PR that would have thrown InvalidOperationException on SignedXml instance reuse in .NET 12 (dotnet/runtime#132836) has been closed and is not going to ship. Remove the 10 entries added across ComputeSignature, CheckSignature, CheckSignatureReturningKey, LoadXml, AddReference, and AddObject, and drop the .NET 12-specific sentence from the `Instances are single-use` remarks section. The general guidance that a SignedXml instance is not intended to be reused across multiple signing or verification operations remains correct and stays. --- xml/System.Security.Cryptography.Xml/SignedXml.xml | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/xml/System.Security.Cryptography.Xml/SignedXml.xml b/xml/System.Security.Cryptography.Xml/SignedXml.xml index 5cae703243c..1022478847a 100644 --- a/xml/System.Security.Cryptography.Xml/SignedXml.xml +++ b/xml/System.Security.Cryptography.Xml/SignedXml.xml @@ -190,7 +190,7 @@ Given the risks of external references, instance is not intended to be reused across multiple signing or verification operations. Starting in .NET 12, calling or more than once on the same instance, or calling , , or after a call to or has begun on the same instance, throws . This applies even if the earlier call itself threw an exception. Create a new instance for each signing or verification operation. +A instance is not intended to be reused across multiple signing or verification operations. Reusing an instance can produce incorrect results. Create a new instance for each signing or verification operation. ]]> @@ -447,7 +447,6 @@ The following code example shows how to sign and verify a single element of an X ]]> - The instance has already been used to compute or verify a signature. @@ -515,7 +514,6 @@ The following code example shows how to sign and verify a single element of an X ]]> - The instance has already been used to compute or verify a signature. @@ -608,7 +606,6 @@ The following code example shows how to sign and verify a single element of an X -or The hash algorithm could not be created. - The instance has already been used to compute or verify a signature. @@ -674,7 +671,6 @@ The following code example shows how to sign and verify a single element of an X -or The hash algorithm could not be created. - The instance has already been used to compute or verify a signature. @@ -740,7 +736,6 @@ The following code example shows how to sign and verify a single element of an X -or- The cryptographic transform used to check the signature could not be created. - The instance has already been used to compute or verify a signature. @@ -803,7 +798,6 @@ The X.509 certificate is verified. The The parameter is . A signature description could not be created for the parameter. - The instance has already been used to compute or verify a signature. @@ -877,7 +871,6 @@ The X.509 certificate is verified. The - The instance has already been used to compute or verify a signature. @@ -955,7 +948,6 @@ The X.509 certificate is verified. The - The instance has already been used to compute or verify a signature. @@ -1026,7 +1018,6 @@ The X.509 certificate is verified. The - The instance has already been used to compute or verify a signature. @@ -1331,7 +1322,6 @@ The X.509 certificate is verified. The parameter does not contain a valid property. - The instance has already been used to compute or verify a signature. @@ -3060,4 +3050,3 @@ The X.509 certificate is verified. The -