Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7656377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:44:56+00:00 2026-05-31T12:44:56+00:00

Here is the entirety of a method inside a class: public Foo GetFooByInfoName(string name)

  • 0

Here is the entirety of a method inside a class:

public Foo GetFooByInfoName(string name)
{
    Debug.Assert(name != null, "name is not an optional argument");
    foreach (Foo f in storedFoos.Values)
    {
        if (name.Equals(f.FooInfo.Name))
        {
            return f;
        }
    }
    return null;
}

I have obviously changed the names, so assume FooInfo is required and Name can’t simply be a property of the class Foo.

Is there a more elegant way to write this method? I’m not very familiar with C# but I feel like there’s an extension method or something which can turn this method into a 2-liner.

Note I’m looking for readability, it’s not a contest to reduce the number of lines. I simply think the method would be more clear if C# had a way to search for the named Foo in one line.

(I’m using C# 3.5)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T12:44:58+00:00Added an answer on May 31, 2026 at 12:44 pm

    return storedFoos.FirstOrDefault(f => name == f.FooInfo.Name);

    You’ll need to make sure that your are using System.Linq.

    By the way, C# overrides the == operator for strings, so it’s usually easier to use == instead of .Equals (much more readable, and idiomatic C#).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package de.tablayoutexample; import java.util.List; import android.os.AsyncTask; import android.util.Log; public class CallWebServiceTask extends AsyncTask<String, String,
I'm not entirely sure whats happened here. I may have messed things up somewhere,
I'm not entirely sure what's breaking here on this page: http://grandoaks.org/floor.html but it seems
I'm not entirely sure if I can do what I want here, but I
Here's the method. I want to know if I am violating any best practices
Possible Duplicate: When to use try/catch blocks? Main method code entirely inside try/catch: Is
I'm not entirely sure how to implement objects in JS. Here is a constructor:
Not entirely sure what's going on here, but I can't seem to call the
So here is my form: class AvatarUploadForm(forms.Form): avatar = forms.ImageField(label='Image', help_text='Maximum size: 100x100px') #
Note * I'm using Factory_girl, not sure if that matters here Currently, I do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.