how to search for a value in a list
pretty stupid question but ive tried looking online and i cant seem to find the answer, im fairly sure im using an older .net
36 Replies
trying to do it on line 26
Unknown User•3y ago
Message Not Public
Sign In & Join Server To View
thanks alot
what does p and first refer too
p => p.Name == attackerName
is a predicate - an anonymous function that takes in an instance of the type the list is generic over, and returns a bool. FirstOrDefault
will return the first item that matches the predicate (it returns true), or default(T)
if no items matchedthanks
ill try it again cause i thought p.Name had to be p.name since in the class it was name
yeah it has to match the casing of the property in the
team_create
class
you really should be using properties instead of fields for Name etc, and use proper casing (PascalCase for classes, properties, methods - camelCase for local variables)
C# doesn't have snake_case
as an officially accepted casing
read more at https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventionsim slightly confused
i looked at the link and im still pretty lost
Well, in coding, we create a lot of things - classes, variables, methods etc
right?
yep
How you name them is important.
ohh
i get what you mean
C# has a "naming convention" which is kind of a standard you should follow
i see
so other people can easily read and understand your code
makes sense
so just capitalise the first letter and stuff
Thats the basics, yeah
so
team_create
would become TeamCreate
snake_case -> PascalCaseI see
oops
public string name;
would become public string Name { get; set; }
that turns it from a public field to a public property, which might seem pointless now but become important later 🙂alr thanks
where would i put that if that makers sense
You'd just replace your existing field
name
with thatohh i was looking in the wrong place
so like this
Yep!
That looks good
one last thing, what should i write in catch so that if the value isnt found in the list it goes back to try or do i use something else
@Pobiega
i tried looking online i found stuff like break but i dont think that will help
you'd need a loop for that
try/catch is "do this, if an exception occurs inside the try, jump to catch"
ah i see
i was trying to make it too complicated lol
sorry to keep asking but how would i convert it to a boolean
wait nvm i cant convert a search into a boolean
you just wanna know if there was a match or not?
Team1.Any(x => x.Name == P1Check)
also, rename your Team1
to team1
:p
its a parameter, they should be camelCase
d
same with Check
, P1Check
etc
anything declared inside the method is camelCasecheckP1 then
sounds good
but yeah,
First
(and FirstOrDefault
) is for finding a match
if you dont want the match returned, just wanna know if it exists, use .Any
thanks
i tend to use firstordefault, then checking if the value is null as my scuffed "exists" check. is Any just better by default from a functional / readability standpoint?
Shorter, returns a bool
And in some cases, like with SQL, it can optimize the query