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 8181397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:30:27+00:00 2026-06-07T00:30:27+00:00

I’m trying to code a Linq MinBy extension method public static class Extensions {

  • 0

I’m trying to code a Linq MinBy extension method

public static class Extensions
{
    public static T MinBy<T>(this IEnumerable<T> source, Func<T,int> selector)
    {
        T min;
        int? minKey = null;
        foreach (var x in source)
        {
            var key = selector(x);
            if (minKey == null || key < minKey)
            {
                minKey = key;
                min = x;
            }
        }
        if (minKey == null)
        {
            throw new ArgumentException("source should not be empty");
        }
        return min;
    }
}

I think my logic is correct and readable. But I get a build error

Use of unassigned local variable ‘min’

What can I do about this? Can I test if the variable is assigned?


Clarification: A MinBy function can answer the following question. Which of the numbers [-5, -2, 3] has the smallest square?

> new List<int>{-5,-2,3}.MinBy(x => x*x)
-2

.NET’s Min function answers a different question (which is the smallest of the squares)

> new List<int>{-5,-2,3}.Min(x => x*x)
4
  • 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-06-07T00:30:29+00:00Added an answer on June 7, 2026 at 12:30 am

    You need a default value for min like this:

    T min = default(T);

    You can read more about default() on MSDN:

    Given a variable t of a parameterized type T, the statement t = null
    is only valid if T is a reference type and t = 0 will only work for
    numeric value types but not for structs. The solution is to use the
    default keyword, which will return null for reference types and zero
    for numeric value types. For structs, it will return each member of
    the struct initialized to zero or null depending on whether they are
    value or reference types. For nullable value types, default returns a
    System.Nullable, which is initialized like any struct.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.