locked
Generics using in method parameter RRS feed

  • Question

  • When using Generics in java I used wild card for method parameter like this  handleList(List<?> list)  in c# it seems no wild card so I tested handleList(object list)  then is works, but I'm not sure

    my question is

    1. Is it a good way ?

    2. If not what can I do?

    thanks in advance

    // Pcdata is my class and no relation with string class, it just example of my code

    static void testGenerices(object list)
            {
                if (list is List<string>)
                {
                    List<string> stringList = (List<string>)list;
                    foreach (string str in stringList)
                    {
                        Console.WriteLine(str);
                    }
                }
                else if (list is List<Pcdata>)
                {
                    List<Pcdata> stringList = (List<Pcdata>)list;
                    foreach (Pcdata pcdata in stringList)
                    {
                        Console.WriteLine(pcdata.ToString());
                    }
                }
            }

            static void testGenerics()
            {
                List<string> list1 = new List<string>();
                List<Pcdata> list2 = new List<Pcdata>();

                list1.Add("1");
                list1.Add("2");

                list2.Add(new Pcdata("pcdata1"));
                list2.Add(new Pcdata("pcdata2"));

                testGenerices(list1);
                testGenerices(list2);
            }

     


    COM, COM+, ATL, STL
    Friday, November 12, 2010 2:32 AM

Answers

  • Be sure to check for null here, and you could use IEnumerable for something as simple as this. 

     

     

    static void TestGenerics<T>(IEnumerable<T> list)
    {
     foreach (T item in list)
     {
      if (item != null) Console.WriteLine(item.ToString());
     }
    }
    Hi Rudy.

     


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light Wiki LinkedInForumsBrowser
    • Proposed as answer by Chrizarus Friday, November 12, 2010 2:31 PM
    • Marked as answer by Rudedog2 Sunday, November 14, 2010 12:05 AM
    Friday, November 12, 2010 2:00 PM
  •  

            static void testGenerices(object list)

            {

            }

            static void TestGenerics<T>(IList<T> list)

            {

                foreach (var item in list )

                {

                    Console.WriteLine(item.ToString());

                }

            }

            static void CallTestGenerics()

            {

                List<string> stringList = new List<string>();

                List<PcData> pcDataList = new List<PcData>();

                TestGenerics<string>(stringList);

                TestGenerics<PcData>(pcDataList);

     

            }

            class PcData

            {

            }

     

    You could also write the method like this.

     

     

            static void TestGenerics<T>(IList<T> list)

            {

                foreach (T item in list )

                {

                    Console.WriteLine(item.ToString());

                }

            }

     

     


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Proposed as answer by Chrizarus Friday, November 12, 2010 2:31 PM
    • Marked as answer by Rudedog2 Sunday, November 14, 2010 12:05 AM
    Friday, November 12, 2010 2:46 AM

All replies

  •  

            static void testGenerices(object list)

            {

            }

            static void TestGenerics<T>(IList<T> list)

            {

                foreach (var item in list )

                {

                    Console.WriteLine(item.ToString());

                }

            }

            static void CallTestGenerics()

            {

                List<string> stringList = new List<string>();

                List<PcData> pcDataList = new List<PcData>();

                TestGenerics<string>(stringList);

                TestGenerics<PcData>(pcDataList);

     

            }

            class PcData

            {

            }

     

    You could also write the method like this.

     

     

            static void TestGenerics<T>(IList<T> list)

            {

                foreach (T item in list )

                {

                    Console.WriteLine(item.ToString());

                }

            }

     

     


    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    • Proposed as answer by Chrizarus Friday, November 12, 2010 2:31 PM
    • Marked as answer by Rudedog2 Sunday, November 14, 2010 12:05 AM
    Friday, November 12, 2010 2:46 AM
  • Be sure to check for null here, and you could use IEnumerable for something as simple as this. 

     

     

    static void TestGenerics<T>(IEnumerable<T> list)
    {
     foreach (T item in list)
     {
      if (item != null) Console.WriteLine(item.ToString());
     }
    }
    Hi Rudy.

     


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light Wiki LinkedInForumsBrowser
    • Proposed as answer by Chrizarus Friday, November 12, 2010 2:31 PM
    • Marked as answer by Rudedog2 Sunday, November 14, 2010 12:05 AM
    Friday, November 12, 2010 2:00 PM
  • Welcome back, David.
    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Friday, November 12, 2010 11:19 PM