Skip to content

Commit fac2678

Browse files
authored
remove console.readline (dotnet#10742)
1 parent acb8274 commit fac2678

File tree

4 files changed

+21
-35
lines changed

4 files changed

+21
-35
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example.Main();
Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,28 @@
1-
using System;
2-
3-
public class Example
1+
public class Example
42
{
53
public static void Main()
64
{
75
// <Snippet5>
8-
Console.Write("Number of random numbers to generate: ");
9-
10-
string? line = Console.ReadLine();
11-
Random rnd = new Random();
6+
Console.WriteLine("Generating 10 random numbers:");
127

13-
if (!int.TryParse(line, out int numbers) || numbers <= 0)
14-
{
15-
numbers = 10;
16-
}
8+
Random rnd = new();
179

18-
for (uint ctr = 1; ctr <= numbers; ctr++)
10+
for (uint ctr = 1; ctr <= 10; ctr++)
1911
Console.WriteLine($"{rnd.Next(),15:N0}");
2012

21-
// The example displays output like the following when asked to generate
22-
// 15 random numbers:
23-
// Number of random numbers to generate: 15
24-
// 367 920 603
25-
// 1 143 790 667
26-
// 1 360 963 275
27-
// 1 851 697 775
28-
// 248 956 796
29-
// 1 009 615 458
30-
// 1 617 743 155
31-
// 1 821 609 652
32-
// 1 661 761 949
33-
// 477 300 794
34-
// 288 418 129
35-
// 425 371 492
36-
// 1 558 147 880
37-
// 1 473 704 017
38-
// 777 507 489
13+
// The example displays output like the following:
14+
//
15+
// Generating 10 random numbers:
16+
// 1,733,189,596
17+
// 566,518,090
18+
// 1,166,108,546
19+
// 1,931,426,514
20+
// 1,532,939,448
21+
// 762,207,767
22+
// 815,074,920
23+
// 1,521,208,785
24+
// 1,950,436,671
25+
// 1,266,596,666
3926
// </Snippet5>
4027
}
4128
}

snippets/csharp/System/Random/Overview/project.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Library</OutputType>
4+
<OutputType>exe</OutputType>
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>

xml/System/Random.xml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,8 @@ The following example uses the parameterless constructor to instantiate three <x
481481
## Remarks
482482
<xref:System.Random.Next%2A?displayProperty=nameWithType> generates a random number whose value ranges from 0 to less than <xref:System.Int32.MaxValue?displayProperty=nameWithType>. To generate a random number whose value ranges from 0 to some other positive number, use the <xref:System.Random.Next%28System.Int32%29?displayProperty=nameWithType> method overload. To generate a random number within a different range, use the <xref:System.Random.Next%28System.Int32%2CSystem.Int32%29?displayProperty=nameWithType> method overload.
483483
484-
485-
486484
## Examples
487-
The following example makes repeated calls to the <xref:System.Random.Next%2A> method to generate a specific number of random numbers requested by the user. The <xref:System.Console.ReadLine%2A?displayProperty=nameWithType> method is used to get customer input.
485+
The following example makes repeated calls to the <xref:System.Random.Next%2A> method to generate 10 random numbers.
488486
489487
:::code language="cpp" source="~/snippets/cpp/VS_Snippets_CLR_System/system.Random.Next/CPP/next3.cpp" id="Snippet5":::
490488
:::code language="csharp" source="~/snippets/csharp/System/Random/Overview/next3.cs" interactive="try-dotnet-method" id="Snippet5":::
@@ -500,7 +498,7 @@ The following example uses the parameterless constructor to instantiate three <x
500498
]]></format>
501499
</remarks>
502500
<block subset="none" type="overrides">
503-
<para>Starting with the .NET Framework version 2.0, if you derive a class from <see cref="T:System.Random" /> and override the <see cref="M:System.Random.Sample" /> method, the distribution provided by the derived class implementation of the <see cref="M:System.Random.Sample" /> method is not used in calls to the base class implementation of the <see cref="M:System.Random.Next" /> method. Instead, the uniform distribution returned by the base <see cref="T:System.Random" /> class is used. This behavior improves the overall performance of the <see cref="T:System.Random" /> class. To modify this behavior to call the <see cref="M:System.Random.Sample" /> method in the derived class, you must also override the <see cref="M:System.Random.Next" /> method.</para>
501+
<para>If you derive a class from <see cref="T:System.Random" /> and override the <see cref="M:System.Random.Sample" /> method, the distribution provided by the derived class implementation of the <see cref="M:System.Random.Sample" /> method is not used in calls to the base class implementation of the <see cref="M:System.Random.Next" /> method. Instead, the uniform distribution returned by the base <see cref="T:System.Random" /> class is used. This behavior improves the overall performance of the <see cref="T:System.Random" /> class. To modify this behavior to call the <see cref="M:System.Random.Sample" /> method in the derived class, you must also override the <see cref="M:System.Random.Next" /> method.</para>
504502
</block>
505503
<altmember cref="T:System.Int32" />
506504
</Docs>

0 commit comments

Comments
 (0)