-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathListing05.19.ReturningAReference.cs
More file actions
42 lines (39 loc) · 1.24 KB
/
Listing05.19.ReturningAReference.cs
File metadata and controls
42 lines (39 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter05.Listing05_19;
using System;
public class Program
{
#region INCLUDE
// Returning a reference
public static ref byte FindFirstRedEyePixel(byte[] image)
{
// Do fancy image detection perhaps with machine learning
for (int counter = 0; counter < image.Length; counter++)
{
if (image[counter] == (byte)ConsoleColor.Red)
{
return ref image[counter];
}
}
throw new InvalidOperationException("No pixels are red.");
}
public static void Main()
{
byte[] image = new byte[254];
// Load image
int index = new Random().Next(0, image.Length - 1);
image[index] =
(byte)ConsoleColor.Red;
Console.WriteLine(
$"image[{index}]={(ConsoleColor)image[index]}");
// ...
#region HIGHLIGHT
// Obtain a reference to the first red pixel
ref byte redPixel = ref FindFirstRedEyePixel(image);
// Update it to be Yellow
redPixel = (byte)ConsoleColor.Yellow;
#endregion HIGHLIGHT
Console.WriteLine(
$"image[{index}]={(ConsoleColor)image[index]}");
}
#endregion INCLUDE
}