A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Linear Search in C# page! Here, you'll find the source code for this program as well as a description of how the program works.
if (
args is not [var input, var targetRaw]
|| !int.TryParse(targetRaw, out int target)
|| !TryParseList(input.AsSpan(), out var numbers)
)
return Usage();
Console.WriteLine(numbers.Contains(target));
return 0;
static bool TryParseList(ReadOnlySpan<char> span, out List<int> numbers)
{
numbers = new(span.Count(',') + 1);
while (!span.IsEmpty)
{
int comma = span.IndexOf(',');
var token = comma >= 0 ? span[..comma] : span;
span = comma >= 0 ? span[(comma + 1)..] : [];
if (!int.TryParse(token, out int n))
return false;
numbers.Add(n);
}
return numbers.Count > 0;
}
static int Usage()
{
Console.WriteLine(
"""Usage: please provide a list of integers ("1, 4, 5, 11, 12") and the integer to find ("11")"""
);
return 1;
}
Linear Search in C# was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.