Results 1 to 12 of 12

Thread: Declaring a method that takes an array of any datetype?

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2018
    Posts
    7
    Rep Power
    0

    Declaring a method that takes an array of any datetype?

    In the code below, I am asking the user to give the dimensions of a multidimensional array.
    The user is then told to enter the values for the array.
    I then want to display the contents of an array by passing it to a method called display.
    The question I have is that how can I make this method accept an array of any data type?
    Say if I create an array that holds string data types and I want to display its contents, then I don't want to create a separate method for this. I want to be able to still pass it to the same method.
    Thanks!



    Code:
    using System;
    public class Program
    {
           public static void Main()
           {
              Console.WriteLine("Enter the number of rows: ");
              int rows = int.Parse(Console.ReadLine());
      
              Console.WriteLine("Enter the number of columns: ");
              int cols = int.Parse(Console.ReadLine());
       
              int[,] matrix = new int[rows,cols];
      
              Console.WriteLine("Enter the values for your matrix");
      
              for(int x=0; x<rows; x++)
              {
                   for(int y=0; y<cols; y++)
                  {
                       matrix[x,y] = int.Parse(Console.ReadLine());
                  }
              }
      
              Program.display(matrix);
           }
    
           public static void display(?? [,] arr)  //what datatype to use here for the array if this method can accept string,float,int etc?
           {
                      for(int x=0; x<arr.GetLength(0); x++)
                      {
                           for(int y=0; y<arr.GetLength(1); y++)
                           {
                               Console.Write(arr[x,y] + " ");
                           }
                      Console.WriteLine();
                      }
            }
    }

  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,915
    Rep Power
    148
    You can write a generic method for this:

    public static void Display<T>(T[,] arr) //what datatype to use here for the array if this method can accept string,float,int etc?
    {
    for(int x=0; x<arr.GetLength(0); x++)
    {
    for(int y=0; y<arr.GetLength(1); y++)
    {
    Console.Write(arr[x,y].ToString() + " ");
    }

    Console.WriteLine();
    }
    }

  3. #3
    .NET Framework
    .NET 4.0
    Join Date
    Apr 2018
    Posts
    7
    Rep Power
    0
    Thank You jmcilhinney!

  4. #4
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2018
    Posts
    8
    Rep Power
    0
    Hi, jmcilhinney
    I'm trying to figure out how to call this method. I tried with:
    Code:
    string[] sa = { "a", "b", "c" };
     Display<string>(sa);
    or
    Code:
    Display<string>(string["a", "b", "c"]);
    But I'm getting errors and I don't quite understand how to pass the arguments.

  5. #5
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,915
    Rep Power
    148
    Quote Originally Posted by Grrr View Post
    Hi, jmcilhinney
    I'm trying to figure out how to call this method. I tried with:
    Code:
    string[] sa = { "a", "b", "c" };
     Display<string>(sa);
    or
    Code:
    Display<string>(string["a", "b", "c"]);
    But I'm getting errors and I don't quite understand how to pass the arguments.
    You create an array in any normal way and then simply call Display and pass the array, e.g.

    var strings = new [] {"a", "b", "c"};

    Display(strings);

    You don't need to specify T explicitly when you call Display because it is inferred from the argument. The parameter is declared to be an array of T so if you pass an array of String then that fixes T to be String.

  6. #6
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2018
    Posts
    8
    Rep Power
    0
    Thanks for answering jmcilhinney ,
    I still get an error (commented it in the code) :

    using System;using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;


    namespace ConsoleApplication4
    {
    class Program
    {
    // [url]http://www.csharpforums.net/showthread.php/3950-Declaring-a-method-that-takes-an-array-of-any-datetype?p=9309#post9309[/url]
    // You can write a generic method for this:
    public static void Display<T>(T[,] arr) // what datatype to use here for the array if this method can accept string,float,int etc?
    {
    for (int x = 0; x < arr.GetLength(0); x++)
    {
    for (int y = 0; y < arr.GetLength(1); y++)
    {
    Console.Write(arr[x, y].ToString() + " ");
    }


    Console.WriteLine();
    }
    }
    static void Main(string[] args)
    {


    var strings = new[] { "a", "b", "c" };


    Display(strings);
    // The above yields the following error:
    /* Error 1
    * The type arguments for method 'ConsoleApplication4.Program.Display<T>(T[*,*])' cannot be inferred from the usage.
    * Try specifying the type arguments explicitly.
    */
    }
    }
    }

  7. #7
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Location
    Norway
    Posts
    423
    Rep Power
    107
    Your request and the Display method posted is for multidimensional array, then later you try to use single dimensional array. If this was the sample it would work as expected:
    var strings = new[,] { { "a", "b" }, { "c", "d" } };
    [xcode=c#] code here [/xcode] - see bbcode list or use formatting buttons in posting editor.

    Visual Studio Community 2017

  8. #8
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2018
    Posts
    8
    Rep Power
    0
    Quote Originally Posted by JohnH View Post
    Your request and the Display method posted is for multidimensional array, then later you try to use single dimensional array. If this was the sample it would work as expected:
    var strings = new[,] { { "a", "b" }, { "c", "d" } };
    Thank you JohnH !
    Indeed now I see that it requires T[,] argument - my eyes are just not adjusted to C#

  9. #9
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,915
    Rep Power
    148
    Quote Originally Posted by Grrr View Post
    Thank you JohnH !
    Indeed now I see that it requires T[,] argument - my eyes are just not adjusted to C#
    Mine too, apparently. I hadn't looked at this thread for a while and I too missed the fact that it was a 2D array. You certainly could reimplement Display for a 1D array.

  10. #10
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2018
    Posts
    8
    Rep Power
    0
    Quote Originally Posted by jmcilhinney View Post
    You certainly could reimplement Display for a 1D array.

    Yes, exactly what I did in order to understand it -

    public static void DisplaySignleArray<T>(T[] arr) // what datatype to use here for the array if this method can accept string,float,int etc?
    {
    for (int i = 0; i < arr.GetLength(0); i++)
    {
    Console.Write(arr[i].ToString() + " ");
    }
    }



    Quote Originally Posted by jmcilhinney View Post
    Mine too, apparently. I hadn't looked at this thread for a while and I too missed the fact that it was a 2D array.

    Yeah, problem is that some of us are too fresh/green (or atleast me), so error-free examples are required at that point (else it becomes too frustrating to debug).
    Last question, under what category of C# is covered this - I assume its the generics one?

  11. #11
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    1,915
    Rep Power
    148
    Quote Originally Posted by Grrr View Post
    Last question, under what category of C# is covered this - I assume its the generics one?
    This is a generic method so that section probably should cover this topic, if not provide an example specifically of using an array.

  12. #12
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2018
    Posts
    8
    Rep Power
    0
    Quote Originally Posted by jmcilhinney View Post
    This is a generic method so that section probably should cover this topic, if not provide an example specifically of using an array.
    Thank you, jmcilhinney !

Similar Threads

  1. Replies: 1
    Last Post: 02-06-2017, 5:34 PM
  2. Replies: 2
    Last Post: 01-21-2016, 1:41 PM
  3. Replies: 3
    Last Post: 12-18-2014, 3:33 AM
  4. Replies: 1
    Last Post: 07-05-2014, 9:34 PM
  5. Question assign an item to a Class Array by reflection if class array is null
    By shiyas in forum C# General Discussion
    Replies: 1
    Last Post: 03-11-2014, 6:45 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •