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.
using System;
using System.Linq;
using System.Collections.Generic;
public class LinearSearch
{
public static bool Search(List<int> list, int toFind)
{
foreach (int value in list)
{
if (value == toFind)
{
return true;
}
}
return false;
}
public static void ErrorAndExit()
{
Console.WriteLine("Usage: please provide a list of integers (\"1, 4, 5, 11, 12\") and the integer to find (\"11\")");
Environment.Exit(1);
}
public static void Main(string[] args)
{
try
{
var list = args[0].Split(',').Select(i => Int32.Parse(i.Trim())).ToList();
var toFind = Int32.Parse(args[1]);
Console.WriteLine(Search(list, toFind));
}
catch
{
ErrorAndExit();
}
}
}
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.