Skip to content

Commit d178e2d

Browse files
authored
Add copilot use case example
1 parent 1385c56 commit d178e2d

File tree

1 file changed

+68
-1
lines changed

1 file changed

+68
-1
lines changed

docs/csharp/programming-guide/types/how-to-convert-a-string-to-a-number.md

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
---
22
title: "How to convert a string to a number"
33
description: Learn how to convert a string to a number in C# by calling the Parse, TryParse, or Convert class methods.
4-
ms.date: 03/15/2024
4+
ms.date: 10/31/2024
55
helpviewer_keywords:
66
- "conversions [C#]"
77
- "conversions [C#], string to int"
88
- "converting strings to int [C#]"
99
- "strings [C#], converting to int"
1010
ms.topic: how-to
11+
ms.collection: ce-skilling-ai-copilot
12+
ms.custom: vs-copilot-horizontal
1113
ms.assetid: 467b9979-86ee-4afd-b734-30299cda91e3
1214
adobe-target: true
1315
---
@@ -50,3 +52,68 @@ The following table lists some of the methods from the <xref:System.Convert> cla
5052
The following example calls the <xref:System.Convert.ToInt32%28System.String%29?displayProperty=nameWithType> method to convert an input string to an [int](../../language-reference/builtin-types/integral-numeric-types.md). The example catches the two most common exceptions thrown by this method: <xref:System.FormatException> and <xref:System.OverflowException>. If the resulting number can be incremented without exceeding <xref:System.Int32.MaxValue?displayProperty=nameWithType>, the example adds 1 to the result and displays the output.
5153

5254
[!code-csharp[Parsing with Convert methods](~/samples/snippets/csharp/programming-guide/string-to-number/convert/program.cs)]
55+
56+
## Use GitHub Copilot to serialize to JSON
57+
58+
You can use GitHub Copilot in your IDE to generate code to convert a string to a number in C#.
59+
60+
If you're using [Visual Studio 2022 version 17.8 or later](/visualstudio/releases/2022/release-notes), you can try the AI-driven [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states) to generate code to convert a string to a number. Submit your question as a prompt in the Copilot chat window, as in the following example. You can also submit prompts using [inline chat](/visualstudio/ide/visual-studio-github-copilot-chat#ask-questions-in-the-inline-chat-view) in the editor window itself.
61+
62+
> [!NOTE]
63+
> GitHub Copilot is powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions. For more information about the general use of GitHub Copilot, product impact, human oversight, and privacy, see [GitHub Copilot FAQs](https://github.com/features/copilot#faq).
64+
65+
The following text shows an example prompt for Copilot Chat:
66+
67+
> Generate code to convert the string "123" to a number. Provide example output.
68+
69+
You can customize the prompt to use a string per your requirements.
70+
71+
You can use chat features, such as [slash commands, references to files, methods, or classes, and threads](/visualstudio/ide/copilot-chat-context), to set intent and get better answers with scoped context. For an existing class in a project that's open in the IDE, you can prompt GitHub Copilot with `/generate code to convert the string "123" to a number`.
72+
73+
The following output shows an example Copilot Chat response:
74+
75+
```output
76+
Here is the code to convert the string "123" to a number using int.TryParse, along with example output:
77+
78+
using System;
79+
80+
class Program
81+
{
82+
static void Main()
83+
{
84+
string str = "123";
85+
int number;
86+
87+
bool isConvertible = int.TryParse(str, out number);
88+
89+
if (isConvertible)
90+
{
91+
// Conversion succeeded, 'number' contains the integer value
92+
Console.WriteLine($"The string was successfully converted to the number: {number}");
93+
}
94+
else
95+
{
96+
// Conversion failed
97+
Console.WriteLine("The string could not be converted to a number.");
98+
}
99+
}
100+
}
101+
102+
**Example Output**
103+
The string was successfully converted to the number: 123
104+
105+
In this example:
106+
• The string "123" is successfully converted to the integer 123.
107+
• The output confirms the successful conversion.
108+
```
109+
110+
When Copilot returns a code block, the response includes options to copy the code, insert the code into a new file, or preview the code output.
111+
112+
> [!NOTE]
113+
> Your results might be different from what's shown in the example responses. AI models are non-deterministic, which means that they can return different responses when asked the same question. This might be due to additional learning and adaption over time, language variation, changes in context, such as your chat history, and more.
114+
115+
For more information, see:
116+
117+
* [GitHub Copilot Trust Center](https://resources.github.com/copilot-trust-center/)
118+
* [GitHub Copilot in Visual Studio](/visualstudio/ide/visual-studio-github-copilot-install-and-states)
119+
* [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview)

0 commit comments

Comments
 (0)