Skip to content

Commit 08162c6

Browse files
v-mepamairaw
andauthored
.NET Interactive: Add try dotnet to batch 21b (#4832)
* add_try_dotnet * Update C# sample * split sample from original source * Split second sample * Update original book * split second example * remove second snippet * Apply suggestions from code review * fix output * fix output Co-authored-by: Maira Wenzel <[email protected]>
1 parent c80405c commit 08162c6

File tree

14 files changed

+382
-390
lines changed

14 files changed

+382
-390
lines changed
Lines changed: 27 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,47 @@
11

22
// <Snippet1>
33
using namespace System;
4-
void main1();
5-
void main2();
6-
void main()
7-
{
8-
main1();
9-
Console::WriteLine();
10-
main2();
11-
}
124

13-
void PrintValues( Array^ myArr );
14-
void main1()
5+
void main()
156
{
167
// Creates and initializes two new Array instances.
17-
Array^ mySourceArray = Array::CreateInstance( String::typeid, 6 );
18-
mySourceArray->SetValue( "three", 0 );
19-
mySourceArray->SetValue( "napping", 1 );
20-
mySourceArray->SetValue( "cats", 2 );
21-
mySourceArray->SetValue( "in", 3 );
22-
mySourceArray->SetValue( "the", 4 );
23-
mySourceArray->SetValue( "barn", 5 );
24-
Array^ myTargetArray = Array::CreateInstance( String::typeid, 15 );
25-
myTargetArray->SetValue( "The", 0 );
26-
myTargetArray->SetValue( "quick", 1 );
27-
myTargetArray->SetValue( "brown", 2 );
28-
myTargetArray->SetValue( "fox", 3 );
29-
myTargetArray->SetValue( "jumps", 4 );
30-
myTargetArray->SetValue( "over", 5 );
31-
myTargetArray->SetValue( "the", 6 );
32-
myTargetArray->SetValue( "lazy", 7 );
33-
myTargetArray->SetValue( "dog", 8 );
8+
Array^ mySourceArray = Array::CreateInstance(String::typeid, 6);
9+
mySourceArray->SetValue("three", 0);
10+
mySourceArray->SetValue("napping", 1);
11+
mySourceArray->SetValue("cats", 2);
12+
mySourceArray->SetValue("in", 3);
13+
mySourceArray->SetValue("the", 4);
14+
mySourceArray->SetValue("barn", 5);
15+
Array^ myTargetArray = Array::CreateInstance(String::typeid, 15);
16+
myTargetArray->SetValue("The", 0);
17+
myTargetArray->SetValue("quick", 1);
18+
myTargetArray->SetValue("brown", 2);
19+
myTargetArray->SetValue("fox", 3);
20+
myTargetArray->SetValue("jumps", 4);
21+
myTargetArray->SetValue("over", 5);
22+
myTargetArray->SetValue("the", 6);
23+
myTargetArray->SetValue("lazy", 7);
24+
myTargetArray->SetValue("dog", 8);
3425

3526
// Displays the values of the Array.
36-
Console::WriteLine( "The target Array instance contains the following (before and after copying):" );
37-
PrintValues( myTargetArray );
27+
Console::WriteLine( "The target Array instance contains the following (before and after copying):");
28+
PrintValues(myTargetArray);
3829

3930
// Copies the source Array to the target Array, starting at index 6.
40-
mySourceArray->CopyTo( myTargetArray, 6 );
31+
mySourceArray->CopyTo(myTargetArray, 6);
4132

4233
// Displays the values of the Array.
43-
PrintValues( myTargetArray );
34+
PrintValues(myTargetArray);
4435
}
4536

46-
void PrintValues( Array^ myArr )
37+
void PrintValues(Array^ myArr)
4738
{
4839
System::Collections::IEnumerator^ myEnumerator = myArr->GetEnumerator();
4940
int i = 0;
50-
int cols = myArr->GetLength( myArr->Rank - 1 );
51-
while ( myEnumerator->MoveNext() )
41+
int cols = myArr->GetLength(myArr->Rank - 1);
42+
while (myEnumerator->MoveNext())
5243
{
53-
if ( i < cols )
44+
if (i < cols)
5445
{
5546
i++;
5647
}
@@ -60,13 +51,12 @@ void PrintValues( Array^ myArr )
6051
i = 1;
6152
}
6253

63-
Console::Write( " {0}", myEnumerator->Current );
54+
Console::Write( " {0}", myEnumerator->Current);
6455
}
6556

6657
Console::WriteLine();
6758
}
6859

69-
7060
/*
7161
This code produces the following output.
7262
@@ -75,63 +65,3 @@ void PrintValues( Array^ myArr )
7565
The quick brown fox jumps over three napping cats in the barn
7666
*/
7767
// </Snippet1>
78-
// <Snippet2>
79-
void PrintIndexAndValues( Array^ myArray );
80-
void main2()
81-
{
82-
// Creates and initializes the source Array.
83-
Array^ myArrayZero = Array::CreateInstance( String::typeid, 3 );
84-
myArrayZero->SetValue( "zero", 0 );
85-
myArrayZero->SetValue( "one", 1 );
86-
87-
// Displays the source Array.
88-
Console::WriteLine( "The array with lowbound=0 contains:" );
89-
PrintIndexAndValues( myArrayZero );
90-
91-
// Creates and initializes the target Array.
92-
array<int>^myArrLen = {4};
93-
array<int>^myArrLow = {2};
94-
Array^ myArrayTwo = Array::CreateInstance( String::typeid, myArrLen, myArrLow );
95-
myArrayTwo->SetValue( "two", 2 );
96-
myArrayTwo->SetValue( "three", 3 );
97-
myArrayTwo->SetValue( "four", 4 );
98-
myArrayTwo->SetValue( "five", 5 );
99-
100-
// Displays the target Array.
101-
Console::WriteLine( "The array with lowbound=2 contains:" );
102-
PrintIndexAndValues( myArrayTwo );
103-
104-
// Copy from the array with lowbound=0 to the array with lowbound=2.
105-
myArrayZero->CopyTo( myArrayTwo, 3 );
106-
107-
// Displays the modified target Array.
108-
Console::WriteLine( "\nAfter copying at relative index 1:" );
109-
PrintIndexAndValues( myArrayTwo );
110-
}
111-
112-
void PrintIndexAndValues( Array^ myArray )
113-
{
114-
for ( int i = myArray->GetLowerBound( 0 ); i <= myArray->GetUpperBound( 0 ); i++ )
115-
Console::WriteLine( "\t[{0}]:\t{1}", i, myArray->GetValue( i ) );
116-
}
117-
118-
/*
119-
This code produces the following output.
120-
121-
The array with lowbound=0 contains:
122-
[0]: zero
123-
[1]: one
124-
[2]:
125-
The array with lowbound=2 contains:
126-
[2]: two
127-
[3]: three
128-
[4]: four
129-
[5]: five
130-
131-
After copying at relative index 1:
132-
[2]: two
133-
[3]: zero
134-
[4]: one
135-
[5]:
136-
*/
137-
// </Snippet2>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// <Snippet1>
2+
using namespace System;
3+
4+
void main()
5+
{
6+
// Creates and initializes the source Array.
7+
Array^ myArrayZero = Array::CreateInstance(String::typeid, 3);
8+
myArrayZero->SetValue("zero", 0);
9+
myArrayZero->SetValue("one", 1);
10+
11+
// Displays the source Array.
12+
Console::WriteLine("The array with lowbound=0 contains:");
13+
PrintIndexAndValues(myArrayZero);
14+
15+
// Creates and initializes the target Array.
16+
array<int>^myArrLen = {4};
17+
array<int>^myArrLow = {2};
18+
Array^ myArrayTwo = Array::CreateInstance(String::typeid, myArrLen, myArrLow);
19+
myArrayTwo->SetValue("two", 2);
20+
myArrayTwo->SetValue("three", 3);
21+
myArrayTwo->SetValue("four", 4);
22+
myArrayTwo->SetValue("five", 5);
23+
24+
// Displays the target Array.
25+
Console::WriteLine("The array with lowbound=2 contains:");
26+
PrintIndexAndValues(myArrayTwo);
27+
28+
// Copy from the array with lowbound=0 to the array with lowbound=2.
29+
myArrayZero->CopyTo(myArrayTwo, 3);
30+
31+
// Displays the modified target Array.
32+
Console::WriteLine("\nAfter copying at relative index 1:");
33+
PrintIndexAndValues(myArrayTwo);
34+
}
35+
36+
void PrintIndexAndValues(Array^ myArray)
37+
{
38+
for (int i = myArray->GetLowerBound(0); i <= myArray->GetUpperBound(0); i++)
39+
Console::WriteLine("\t[{0}]:\t{1}", i, myArray->GetValue(i));
40+
}
41+
42+
/*
43+
This code produces the following output.
44+
45+
The array with lowbound=0 contains:
46+
[0]: zero
47+
[1]: one
48+
[2]:
49+
The array with lowbound=2 contains:
50+
[2]: two
51+
[3]: three
52+
[4]: four
53+
[5]: five
54+
55+
After copying at relative index 1:
56+
[2]: two
57+
[3]: zero
58+
[4]: one
59+
[5]:
60+
*/
61+
// </Snippet1>

0 commit comments

Comments
 (0)