Click here to Skip to main content
14,865,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In the below code I want to perform the generics add of int array as well as add of string array.
so how to achieve this by using generics.
class Program
        static void Main(string[] args)
            int[] x = new int[] { 10, 20, 30, 40 };
            string[] y = new string[] { "Himanshu", "Panjabi" };
            ArrayCalc<int> ac1 = new ArrayCalc<int>();
            int r1 = ac1.Add(x);

            //ArrayCalc<string> ac2=new ArrayCalc<string>();
            //string r2=ac2.Add(y);

            //int[] a = new int[] { 2, 4, 6, 8, 10 };
            //int res = a.Sum();
    public class ArrayCalc<T> where T : IComparable
        public T Add(T[] t1)
            T t3 = default(T);
            if (t1 is int[])
                int[] num = new int[10];
                int res = Convert.ToInt32(num.Sum());
                //int asum=num.Sum();
                //int num1 =Convert.ToInt32(t1);
                // int num2 = Convert.ToInt32(t2);
                //int res = num1;
                T files = (T)System.Convert.ChangeType(res, typeof(T));
                return files;
            //else if (t1 is string)
            //    return (T)System.Convert.ChangeType(x, typeof(T));
                return (T)System.Convert.ChangeType(t3, typeof(T));

Updated 28-Apr-15 0:06am

Try this

public T Add(T[] t1)
           T t3 = default(T);
           if (t1 is int[])
               int[] temp = t1 as int[];
               int[] num = new int[10];
               var res = temp.Sum();
Member 11132163 28-Apr-15 6:14am
Thank You for soling my first doubt
now my second doubt is how to perform add in string array??
Karthik_Mahalingam 28-Apr-15 6:16am
how will u add string values? does it make sense ?
add or concatenate ??
be specific in the question.
Member 11132163 28-Apr-15 6:21am
I mean how to do concatenation of string values
so in the program I want the following type of output HimanshuPanjabi
Karthik_Mahalingam 28-Apr-15 7:42am
Sascha Lefévre solution has the rest of the answer. you can refer that.
First, I must tell you that your idea of that ArrayCalc-class isn't a good use for a generic class. The main benefit of generics is that you can use exactly the same code for various types. If you have to make a case-distinction in your code between different types to make it work, that benefit is lost. In those cases it makes much more sense to implement the logic separately per type.

However, just to show you how your class could be implemented, here's my take:
using System;
using System.Linq;
public class Program
    public static void Main()
        int[] integers = new int[] { 10, 20, 30, 40 };
        string[] strings = new string[] { "Himanshu", "Panjabi" };

        ArrayCalc<int> calcInt = new ArrayCalc<int>();
        int sum = calcInt.Add(integers);

        ArrayCalc<string> calcString = new ArrayCalc<string>();
        string joined = calcString.Add(strings);

    public class ArrayCalc<T>
        public T Add(T[] array)
            if (array is int[])
                int[] numbers = array as int[];
                int sum = numbers.Sum();
                return (T)(object)sum;       // <-- UGLY!
            else if (array is string[])
                string[] strings = array as string[];
                string joined = String.Join("", strings);
                return (T)(object)joined;    // <-- UGLY!
                throw new NotSupportedException("can't add " + typeof(T).Name);


As indicated by "UGLY!", this mis-use (to be honest) of generics where you make case-distinctions based on the generic type also leads to neccessary ugly reverse-casts to the generic type T in order to be able to return the result without the compiler complaining about it.
An ArrayList does not support generics so you cannot create any ArrayList of a type for e.g. int or string.

Use List<t> instead.

For e.g. List<int> coll = new List<int>();

Here is a difference between both of them -[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900