How to make my function more generic ??

Let's say i have these 2 function

void foo1()
{
Class1 classArray[] = GetArray();
for(int i=0;i<classArray.Lenght;i++)
{
console.WriteLine(classArrayIdea);
}
}

void foo2()
{
Class2 classArray[] = GetArray();
for(int i=0;i<classArray.Lenght;i++)
{
console.WriteLine(classArrayIdea);
}
}

as you can see .. the 2 function does exactly the same thing .. the only difference is the objet class1, class2 .... its just an illustration of my problem .... id like to do a more generic function... possible


Answer this question

How to make my function more generic ??

  • R_Vogel

    its all good Thanks!

    I didnt put <T> near the function name

  • Hrishikesh

    I believe this falls under the category of polymorphism.

    I haven't worked much with polymorphism in C#, but I believe the principles should be the same as other languages.

    If you derive a class from another type, you can use that more generic (or abstract) type to reference that object. Lets say we have an abstract class Shape, you could inherit the characteristics of that abstract class into your classes.

    class Oval : Shape {...};
    class Rectangle : Shape {...};
    class Square : Rectangle {...};
    class Circle : Oval {...};

    etc...

    and now you can have one generic function which will act on a Shape object:

    Circle c;
    Square sq;
    Shape s = c;
    Shape s2 = sq;

    GenericFoo(s);
    GenericFoo(s2);

    void GenericFoo(Shape s)
    {
    Shape shapeArray[] = GetArray();
    for(int n=0;n<shapeArray.Lenght;n++)
    {
    console.WriteLine(shapeArrayNo);
    }
    }

    I can't test this right now but I hope this gets the idea across.


  • arkiboys

    If you want to use generics, the following code will take an array of most any type and print out the class name:

        void UseArray<T>(T[] classArray)
        {
          foreach (T classInstance in classArray)
          {
            Console.WriteLine(classInstance.GetType().Name);
          }
        }
    

    Here is a complete example:

    namespace ConsoleApplication3
    {
      // Declare two classes
      class Class1 { } 
      class Class2 { }
    
      class Program
      {
        // Initialize and return the classes in an array
        public Object[] GetArray(int option)
        {
          Object[] myArray;
          if (option == 1) return new Class1[2] { new Class1(), new Class1() };
          else return new Class2[2] { new Class2(), new Class2() };      
        }
    
        // One function to use both types of arrays
        void UseArray<T>(T[] classArray)
        {
          foreach (T classInstance in classArray)
          {
            Console.WriteLine(classInstance.GetType().Name);
          }
        }
    
        // Code to call UseArray
        static void Main(string[] args)
        {
          Program program = new Program();
          Object[] classArray;
          classArray = program.GetArray(1);
          program.UseArray((Class1[])classArray);
          classArray = program.GetArray(2);
          program.UseArray((Class2[])classArray);
          Console.ReadLine();
        }
      }
    }
     


  • asisurfer

    do i need some using to use T

  • How to make my function more generic ??