Linq is not necessarily faster: it's a myth.
Linq use a lot of methods calls that take huge cpu cycles...
Certainly Linq is optimized and can make prousesses in a few calls that can sometimes be faster than code even written by an insider.
But depending on the type of objects you're acting on, Linq requires a lot of resources and will sometimes be slower.
The question to ask with Linq is above all about the quality of the code, its brevity, its robustness and its maintainability, and here it is a marvel.
For your code, you don't need Linq and it will not go faster.
You can add a break at the end of the if
scope, so it stops the parsing after found the user:
foreach (var user in elite.Users)
if ( user.UserName == txtuser.Text && user.UserPWD == txtpass.Text )
{
MainForm mainForm = new MainForm();
mainForm.ShowDialog();
Hide();
Close();
break;
}
Using Linq, you can write:
if ( elite.Users.Any(user => user.UserName == txtuser.Text
&& user.UserPWD == txtpass.Text) )
{
MainForm mainForm = new MainForm();
mainForm.ShowDialog();
Hide();
Close();
}
As you see, you have no gain here with the code itself and it won't execute faster, nor slower, on a such simple thing.
Because, what does Linq ? It does a foreach somehow...
https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,e73922753675387a,references
https://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,8087366974af11d2