Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

linear search: practical reference

linear search: practical reference: Linear search examines elements in sequence until a match or the end.

Version: C# language and .NET 10 reference runtime

Syntax

// linear search

Linear search examines elements in sequence until a match or the end.

Use it for small or one-off sequences without an index.

Repeated O(n) searches can justify a Dictionary or HashSet.

Represent no result without conflict with a valid index or value.

The complete program actually uses linear search; its exact expected stdout is 1.

Examples

using System;
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Number = System.Int32;

var values = new[] { 4, 7, 9 }; var index = Array.FindIndex(values, value => value == 7); Console.WriteLine(index);

Result

1

The complete program actually uses `linear search`; its exact expected stdout is `1`.

Sources