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

  • Home
  • SEARCH
  • 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 7821405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:38:27+00:00 2026-06-02T07:38:27+00:00

Further to my questions https://stackoverflow.com/questions/10241345/any-percentage-function-in-net https://stackoverflow.com/questions/9942202/percentage-of-each-element-in-linq i want to create a generic extension method

  • 0

Further to my questions

https://stackoverflow.com/questions/10241345/any-percentage-function-in-net

https://stackoverflow.com/questions/9942202/percentage-of-each-element-in-linq

i want to create a generic extension method in .Net 4.0 which will generate percentage of the list items to the sum of the list. This list will be of Numeric types only.

My code is

public static T Percentage<T>(this T array) where T: ? // What should come in place of ? 
{
  double[] darray = (double[])T; // Error convert type 'T' to 'double[]'

  darray = darray.Select(x=> x * 100 / darray.Sum()).ToArray();

  return darray; // Error convert type 'double[]' to 'T'
}

I want to call it like

double[] MD = {8.0,21.0,25.0,13.0,26.0,37.0,37.0,33.0,71.0,9.0};

MD = MD.Percentage().ToArray(); 

What am i doing wrong;

  • 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-02T07:38:29+00:00Added an answer on June 2, 2026 at 7:38 am

    You cannot constrain types by “numeric” values. It is somewhat of a limitation of the framework.

    Your best best is to do this:

    public static IEnumerable<double> Percentage(this IEnumerable<int> values)
    {
        var sum = values.Sum();
        return values.Select(v => (double)v / (double)sum).ToArray();
    }
    
    public static IEnumerable<double> Percentage(this IEnumerable<long> values)
    {
        var sum = values.Sum();
        return values.Select(v => (double)v / (double)sum).ToArray();
    }
    
    public static IEnumerable<double> Percentage(this IEnumerable<double> values)
    {
        var sum = values.Sum();
        return values.Select(v => v / sum).ToArray();
    }
    

    Essentially you need to produce an overload for each type you want to support.

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

Sidebar

Related Questions

Further to https://stackoverflow.com/questions/8990246/frame-like-in-html/8990407#8990407 I implemented floating divs in my JSP. It works properly in
It is a question build upon the previous question (http://stackoverflow.com/questions/6538448/r-how-to-write-a-loop-to-get-a-matrix). It is different from
I just had some basic php questions to further my understanding as I learn,
Further to my question on disabling validators using javascript - Disable ASP.NET validators with
I have been looking at the YouTube Insight function and want to learn how
Im using this code http://platinumdogs.wordpress.com/2008/10/30/net-c-impersonation-with-network-credentials/#comment-770 to connect from a machine in the domain to
I'm trying to implement this project: http://img7.imagebanana.com/img/cnb46ti2/relationships.png I want to let view the skills
This is a further question based on this answer: How can I implement a
Background (question further down) I've been Googling this back and forth reading RFCs and
To continue to question further I'm more interested in blogs, websites who once in

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.