Skip to content

Commit 92a488b

Browse files
authored
remove string.copy from snippets (#10190)
1 parent 5b08103 commit 92a488b

File tree

28 files changed

+437
-359
lines changed

28 files changed

+437
-359
lines changed
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
// <Snippet2>
1+
// <Snippet2>
22
using System;
33

44
public class TestAction4
55
{
6-
public static void Main()
7-
{
8-
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
9-
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
10-
string[] copiedOrdinals = new string[ordinals.Length];
11-
Action<string[], string[], int, int> copyOperation = CopyStrings;
12-
copyOperation(ordinals, copiedOrdinals, 3, 5);
13-
foreach (string ordinal in copiedOrdinals)
14-
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
15-
}
6+
public static void Main()
7+
{
8+
string[] ordinals = ["First", "Second", "Third", "Fourth", "Fifth",
9+
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"];
10+
string[] copiedOrdinals = new string[ordinals.Length];
11+
Action<string[], string[], int, int> copyOperation = CopyStrings;
12+
copyOperation(ordinals, copiedOrdinals, 3, 5);
1613

17-
private static void CopyStrings(string[] source, string[] target,
18-
int startPos, int number)
19-
{
20-
if (source.Length != target.Length)
21-
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
14+
Console.WriteLine();
15+
foreach (string ordinal in copiedOrdinals)
16+
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
17+
}
2218

23-
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
24-
target[ctr] = string.Copy(source[ctr]);
25-
}
19+
private static void CopyStrings(string[] source, string[] target,
20+
int startPos, int number)
21+
{
22+
if (source.Length != target.Length)
23+
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
24+
25+
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
26+
target[ctr] = source[ctr];
27+
}
2628
}
2729
// </Snippet2>
Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
// <Snippet3>
1+
// <Snippet3>
22
using System;
33

44
public class TestAnonymousMethod
55
{
6-
public static void Main()
7-
{
8-
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
9-
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
10-
string[] copiedOrdinals = new string[ordinals.Length];
11-
Action<string[], string[], int, int> copyOperation =
12-
delegate(string[] s1, string[] s2,
13-
int pos, int num)
14-
{ CopyStrings(s1, s2, pos, num); };
15-
copyOperation(ordinals, copiedOrdinals, 3, 5);
16-
foreach (string ordinal in copiedOrdinals)
17-
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
18-
}
6+
public static void Main()
7+
{
8+
string[] ordinals = ["First", "Second", "Third", "Fourth", "Fifth",
9+
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"];
10+
string[] copiedOrdinals = new string[ordinals.Length];
11+
Action<string[], string[], int, int> copyOperation =
12+
delegate (string[] s1, string[] s2,
13+
int pos, int num)
14+
{ CopyStrings(s1, s2, pos, num); };
15+
copyOperation(ordinals, copiedOrdinals, 3, 5);
1916

20-
private static void CopyStrings(string[] source, string[] target,
21-
int startPos, int number)
22-
{
23-
if (source.Length != target.Length)
24-
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
17+
Console.WriteLine();
18+
foreach (string ordinal in copiedOrdinals)
19+
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
20+
}
2521

26-
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
27-
target[ctr] = string.Copy(source[ctr]);
28-
}
22+
private static void CopyStrings(string[] source, string[] target,
23+
int startPos, int number)
24+
{
25+
if (source.Length != target.Length)
26+
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
27+
28+
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
29+
target[ctr] = source[ctr];
30+
}
2931
}
3032
// </Snippet3>
Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <Snippet1>
1+
// <Snippet1>
22
using System;
33

44
delegate void StringCopy(string[] stringArray1,
@@ -8,25 +8,27 @@ delegate void StringCopy(string[] stringArray1,
88

99
public class TestDelegate
1010
{
11-
public static void Main()
12-
{
13-
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
14-
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
15-
string[] copiedOrdinals = new string[ordinals.Length];
16-
StringCopy copyOperation = CopyStrings;
17-
copyOperation(ordinals, copiedOrdinals, 3, 5);
18-
foreach (string ordinal in copiedOrdinals)
19-
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
20-
}
11+
public static void Main()
12+
{
13+
string[] ordinals = ["First", "Second", "Third", "Fourth", "Fifth",
14+
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"];
15+
string[] copiedOrdinals = new string[ordinals.Length];
16+
StringCopy copyOperation = CopyStrings;
17+
copyOperation(ordinals, copiedOrdinals, 3, 5);
2118

22-
private static void CopyStrings(string[] source, string[] target,
23-
int startPos, int number)
24-
{
25-
if (source.Length != target.Length)
26-
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
19+
Console.WriteLine();
20+
foreach (string ordinal in copiedOrdinals)
21+
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
22+
}
2723

28-
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
29-
target[ctr] = string.Copy(source[ctr]);
30-
}
24+
private static void CopyStrings(string[] source, string[] target,
25+
int startPos, int number)
26+
{
27+
if (source.Length != target.Length)
28+
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
29+
30+
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
31+
target[ctr] = source[ctr];
32+
}
3133
}
3234
// </Snippet1>
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1-
// <Snippet4>
1+
// <Snippet4>
22
using System;
33

44
public class TestLambdaExpression
55
{
6-
public static void Main()
7-
{
8-
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth",
9-
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"};
10-
string[] copiedOrdinals = new string[ordinals.Length];
11-
Action<string[], string[], int, int> copyOperation = (s1, s2, pos, num)
12-
=> CopyStrings(s1, s2, pos, num);
13-
copyOperation(ordinals, copiedOrdinals, 3, 5);
14-
foreach (string ordinal in copiedOrdinals)
15-
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
16-
}
6+
public static void Main()
7+
{
8+
string[] ordinals = ["First", "Second", "Third", "Fourth", "Fifth",
9+
"Sixth", "Seventh", "Eighth", "Ninth", "Tenth"];
10+
string[] copiedOrdinals = new string[ordinals.Length];
11+
Action<string[], string[], int, int> copyOperation = (s1, s2, pos, num)
12+
=> CopyStrings(s1, s2, pos, num);
13+
copyOperation(ordinals, copiedOrdinals, 3, 5);
1714

18-
private static void CopyStrings(string[] source, string[] target,
19-
int startPos, int number)
20-
{
21-
if (source.Length != target.Length)
22-
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
15+
Console.WriteLine();
16+
foreach (string ordinal in copiedOrdinals)
17+
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
18+
}
2319

24-
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
25-
target[ctr] = string.Copy(source[ctr]);
26-
}
20+
private static void CopyStrings(string[] source, string[] target,
21+
int startPos, int number)
22+
{
23+
if (source.Length != target.Length)
24+
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
25+
26+
for (int ctr = startPos; ctr <= startPos + number - 1; ctr++)
27+
target[ctr] = source[ctr];
28+
}
2729
}
2830
// </Snippet4>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
TestAction4.Main();
2+
TestAnonymousMethod.Main();
3+
TestLambdaExpression.Main();
4+
TestDelegate.Main();

snippets/csharp/System/ActionT1,T2,T3,T4/Overview/Project.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>Library</OutputType>
5-
<TargetFramework>net6.0</TargetFramework>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
66

77
</PropertyGroup>
88

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
// <Snippet2>
1+
// <Snippet2>
22
using System;
33

44
public class TestAction3
55
{
6-
public static void Main()
7-
{
8-
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};
9-
string[] copiedOrdinals = new string[ordinals.Length];
10-
Action<string[], string[], int> copyOperation = CopyStrings;
11-
copyOperation(ordinals, copiedOrdinals, 3);
12-
foreach (string ordinal in copiedOrdinals)
13-
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
14-
}
6+
public static void Main()
7+
{
8+
string[] ordinals = ["First", "Second", "Third", "Fourth", "Fifth"];
9+
string[] copiedOrdinals = new string[ordinals.Length];
10+
Action<string[], string[], int> copyOperation = CopyStrings;
11+
copyOperation(ordinals, copiedOrdinals, 3);
12+
foreach (string ordinal in copiedOrdinals)
13+
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
14+
}
1515

16-
private static void CopyStrings(string[] source, string[] target, int startPos)
17-
{
18-
if (source.Length != target.Length)
19-
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
16+
private static void CopyStrings(string[] source, string[] target, int startPos)
17+
{
18+
if (source.Length != target.Length)
19+
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
2020

21-
for (int ctr = startPos; ctr <= source.Length - 1; ctr++)
22-
target[ctr] = string.Copy(source[ctr]);
23-
}
21+
for (int ctr = startPos; ctr <= source.Length - 1; ctr++)
22+
target[ctr] = source[ctr];
23+
}
2424
}
2525
// </Snippet2>
Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
// <Snippet3>
1+
// <Snippet3>
22
using System;
33

44
public class TestAnon
55
{
6-
public static void Main()
7-
{
8-
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};
9-
string[] copiedOrdinals = new string[ordinals.Length];
10-
Action<string[], string[], int> copyOperation = delegate(string[] s1,
11-
string[] s2,
12-
int pos)
13-
{ CopyStrings(s1, s2, pos); };
14-
copyOperation(ordinals, copiedOrdinals, 3);
15-
foreach (string ordinal in copiedOrdinals)
16-
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
17-
}
6+
public static void Main()
7+
{
8+
string[] ordinals = ["First", "Second", "Third", "Fourth", "Fifth"];
9+
string[] copiedOrdinals = new string[ordinals.Length];
10+
Action<string[], string[], int> copyOperation = delegate (string[] s1,
11+
string[] s2,
12+
int pos)
13+
{ CopyStrings(s1, s2, pos); };
14+
copyOperation(ordinals, copiedOrdinals, 3);
15+
foreach (string ordinal in copiedOrdinals)
16+
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
17+
}
1818

19-
private static void CopyStrings(string[] source, string[] target, int startPos)
20-
{
21-
if (source.Length != target.Length)
22-
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
19+
private static void CopyStrings(string[] source, string[] target, int startPos)
20+
{
21+
if (source.Length != target.Length)
22+
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
2323

24-
for (int ctr = startPos; ctr <= source.Length - 1; ctr++)
25-
target[ctr] = string.Copy(source[ctr]);
26-
}
24+
for (int ctr = startPos; ctr <= source.Length - 1; ctr++)
25+
target[ctr] = source[ctr];
26+
}
2727
}
2828
// </Snippet3>
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// <Snippet1>
1+
// <Snippet1>
22
using System;
33

44
delegate void StringCopy(string[] stringArray1,
@@ -7,23 +7,23 @@ delegate void StringCopy(string[] stringArray1,
77

88
public class TestDelegate
99
{
10-
public static void Main()
11-
{
12-
string[] ordinals = {"First", "Second", "Third", "Fourth", "Fifth"};
13-
string[] copiedOrdinals = new string[ordinals.Length];
14-
StringCopy copyOperation = CopyStrings;
15-
copyOperation(ordinals, copiedOrdinals, 3);
16-
foreach (string ordinal in copiedOrdinals)
17-
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
18-
}
10+
public static void Main()
11+
{
12+
string[] ordinals = ["First", "Second", "Third", "Fourth", "Fifth"];
13+
string[] copiedOrdinals = new string[ordinals.Length];
14+
StringCopy copyOperation = CopyStrings;
15+
copyOperation(ordinals, copiedOrdinals, 3);
16+
foreach (string ordinal in copiedOrdinals)
17+
Console.WriteLine(string.IsNullOrEmpty(ordinal) ? "<None>" : ordinal);
18+
}
1919

20-
private static void CopyStrings(string[] source, string[] target, int startPos)
21-
{
22-
if (source.Length != target.Length)
23-
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
20+
private static void CopyStrings(string[] source, string[] target, int startPos)
21+
{
22+
if (source.Length != target.Length)
23+
throw new IndexOutOfRangeException("The source and target arrays must have the same number of elements.");
2424

25-
for (int ctr = startPos; ctr <= source.Length - 1; ctr++)
26-
target[ctr] = string.Copy(source[ctr]);
27-
}
25+
for (int ctr = startPos; ctr <= source.Length - 1; ctr++)
26+
target[ctr] = source[ctr];
27+
}
2828
}
2929
// </Snippet1>

0 commit comments

Comments
 (0)