C
C#6mo ago
LeviCoding

Fast search an object in a list using his attribute

So I have a list of ships and each ship has a length width, mass and a name. We need to write a method that searches in this list the oobject using his name attribute. So only a string is given with the method. for example public Ship SearchShip(string shipName){}
3 Replies
LeviCoding
LeviCoding6mo ago
c#
public abstract class Schip
{
private double _lengte;
public double Lengte { get; set; }

private double _breedte;
public double Breedte { get; set;}

private double _tonnage;
public double Tonnage { get; set; }

private string _naam;
public string Naam { get; set; }

public Schip(double lengte, double breedte, double tonnage, string naam)
{
Lengte=lengte;
Breedte=breedte;
Tonnage=tonnage;
Naam=naam;
}
}
c#
public abstract class Schip
{
private double _lengte;
public double Lengte { get; set; }

private double _breedte;
public double Breedte { get; set;}

private double _tonnage;
public double Tonnage { get; set; }

private string _naam;
public string Naam { get; set; }

public Schip(double lengte, double breedte, double tonnage, string naam)
{
Lengte=lengte;
Breedte=breedte;
Tonnage=tonnage;
Naam=naam;
}
}
c#
public Ship SearchShip(List<Ship> ships, string shipName){}
c#
public Ship SearchShip(List<Ship> ships, string shipName){}
We can not use lambdas, cuz we only learn it later in our course.
joren
joren6mo ago
iterate over the ships and compare the Naam with the shipName
public static Schip SearchShip(List<Schip> ships, string shipName)
{
foreach (Schip ship in ships)
{
if (ship.Naam.Equals(shipName, StringComparison.OrdinalIgnoreCase))
return ship;
}
return null; // or throw an exception, whatever your assignment states
}
public static Schip SearchShip(List<Schip> ships, string shipName)
{
foreach (Schip ship in ships)
{
if (ship.Naam.Equals(shipName, StringComparison.OrdinalIgnoreCase))
return ship;
}
return null; // or throw an exception, whatever your assignment states
}
(unhide to see an example of an solution)
LeviCoding
LeviCoding6mo ago
Is the use of a dictionary not faster? and using the name of the object as a key
Want results from more Discord servers?
Add your server
More Posts
Need authentication from MVC to WebAPIHi all, Can someone point me in the right direction here. I want to do something simple. I want to sVisual Studio doesnt show errors and projectsHey, I encountered a small issue with Visual Studio Community 2022 - it doesn't show projects in solAsp.Net Core How to create PDF Product Catalog With a template with an image in the backgroundI am developing a web application with Asp.net. The application will select the products on the e-coBest Way to store data for an Discord BotHey everyone, i need some input from some experts 😉 I am trying to build an Discord Bot who gives Rjust begun learning ASP.NET Core MVC and am looking for any good material to help mei am very new to ASP.Net so am just looking for some beginner friendly resources to begin properly lHello. I am facing an issue with null exception after calling OnPost method in my razor pages site.When i call method OnGet i load all of the information about property. public void OnGet(int✅ hey. im new to learning asp.net and was wondering if anyone had any suggestions as where to start?just any ideas of things to do/ things to watch would be great!Measuring sub-millisecond execution time with Methodtimer.FodyI am using https://github.com/Fody/MethodTimer to measure code execution time. The function I am try✅ How to SimplifyI have this https://gist.github.com/IXLLEGACYIXL/399c51cf0341188bdc4e02a820c34ce0 I need the generic✅ Blazorserver - Value after manual db-update not changing after new fetchHello, I setup a blazorserver project and implemented a db connection and wanted to load user data.