Skip to content

Commit e9f7aed

Browse files
pkulikovRon Petrusha
authored andcommitted
Added snippets for element access operator (#559)
1 parent e30a306 commit e9f7aed

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace operators
5+
{
6+
public static class IndexOperatorExamples
7+
{
8+
public static void Examples()
9+
{
10+
Arrays();
11+
Indexers();
12+
}
13+
14+
private static void Arrays()
15+
{
16+
// <SnippetArrays>
17+
int[] fib = new int[10];
18+
fib[0] = fib[1] = 1;
19+
for (int i = 2; i < fib.Length; i++)
20+
{
21+
fib[i] = fib[i - 1] + fib[i - 2];
22+
}
23+
Console.WriteLine(fib[fib.Length - 1]); // output: 55
24+
25+
double[,] matrix = new double[2,2];
26+
matrix[0,0] = 1.0;
27+
matrix[0,1] = 2.0;
28+
matrix[1,0] = matrix[1,1] = 3.0;
29+
var determinant = matrix[0,0] * matrix[1,1] - matrix[1,0] * matrix[0,1];
30+
Console.WriteLine(determinant); // output: -3
31+
// </SnippetArrays>
32+
}
33+
34+
private static void Indexers()
35+
{
36+
// <SnippetIndexers>
37+
var dict = new Dictionary<string, double>();
38+
dict["one"] = 1;
39+
dict["pi"] = Math.PI;
40+
Console.WriteLine(dict["one"] + dict["pi"]);
41+
// </SnippetIndexers>
42+
}
43+
}
44+
}

snippets/csharp/language-reference/operators/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ static void Main(string[] args)
4949
Console.WriteLine("======= >, <, >=, and <= operator examples =====");
5050
GreaterAndLessOperatorsExamples.Examples();
5151
Console.WriteLine();
52+
53+
Console.WriteLine("============== [] operator examples ============");
54+
IndexOperatorExamples.Examples();
55+
Console.WriteLine();
5256
}
5357
}
5458
}

0 commit comments

Comments
 (0)