Click here to Skip to main content
15,894,546 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.
C#
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);

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

            //int[] a = new int[] { 2, 4, 6, 8, 10 };
            //int res = a.Sum();
            //Console.WriteLine(res);
            Console.ReadLine();
        }
    }
    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));
            //}
            else
                return (T)System.Convert.ChangeType(t3, typeof(T));


        }
Posted
Updated 28-Apr-15 0:06am
v2

Try this


C#
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();
 
Share this answer
 
Comments
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.
Thanks
Member 11132163 28-Apr-15 6:21am    
sorry!!
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:
C#
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);
        Console.WriteLine(sum);

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

    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!
            }
            else
                throw new NotSupportedException("can't add " + typeof(T).Name);
        }
    }
}

Console-output:
100
HimanshuPanjabi


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.
 
Share this answer
 
v2
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 - http://www.aspsnippets.com/Articles/Difference-between-ArrayList-and-Generic-List-in-C-Net-and-VBNet.aspx[^].
 
Share this answer
 
v3

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