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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:16:29+00:00 2026-05-31T05:16:29+00:00

I was supposed to write a method, which will perform addition on collection of

  • 0

I was supposed to write a method, which will perform addition on collection of integer, float or double. I was going to write a three methods which will traverse three different types perform addition and return the value. It works. I am just curious, Is this can be done in a single method, where the type is passed a a generic type, something like

public static T SUM<T>(IEnumerable<T> dataCollection)
{
T total;
foreach(var value in dataCollection)
total += value;
return total;
}

I was able to resolve it with normal three method implementation but just curious, is it even possible?

Thanks,

  • 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-31T05:16:29+00:00Added an answer on May 31, 2026 at 5:16 am

    It’s not possible to use operators like + / - / * / etc ... on expressions which are typed to generic type parameters. These only work when dealing with concrete types.

    To do this with generics you’ll need to pass in an abstraction which understands how to do operations like + on the actual type of T. For example, here’s a lambda example

    public static T SUM<T>(IEnumerable<T> dataCollection, Func<T, T, T> sum)
    {
      T total = default(T);
      foreach(var value in dataCollection)
        total = sum(total, value);
      return total;
    }
    
    List<int> list = ...;
    list.Sum((left, right) -> left + right);
    

    Note: As several have pointed out the function I wrote here is essentially identical to System.Linq.Enumerable.Aggregate. Please use that vs. putting this in your code.

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

Sidebar

Related Questions

I am supposed to write a client application in C# that communicates with Thales
I am supposed to write a program in JavaScript to find all the anagrams
I am supposed to write a program that gets some PNG images from the
Given the following code (it's supposed to write helloworld in a helloworld file, and
Java is supposed to be write once, run anywhere and it really can be,
I need to write a java script. This is supposed to validate if the
Here's some simple code that, for each argument specified, will add specific get/set methods
Suppose we have some resource(a file on disk) in which we have to write
I tried to programtically write a method and assign it as a method called
I have a homework assignment that I am supposed to write a http server

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.