Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 164835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T11:48:52+00:00 2026-05-11T11:48:52+00:00

I have a requirement where I can get the following in an object –

  • 0

I have a requirement where I can get the following in an object –

a type T or List<T>  

Converting object into T is easy. How can I convert it to List(by first checking that it can be converted successfully or not), reason I want to convert is to scroll through the list and call tostring on each element.

My actual code –

namespace Generic_Collection_Code {     class Program     {         public static string DumpObj(object obj)         {             string sTemp = String.Empty;              List<int> ints = obj as List<int>;             if (ints != null)             {                 foreach (int i in ints)                     sTemp += i.ToString() + ',';                 sTemp.Trim(',');             }             else              {                 List<string> strings = obj as List<string>;                 if (strings != null)                 {                     foreach (string s in strings)                         sTemp += s + ',';                     sTemp.Trim(',');                 }                 else                 {                     sTemp += obj.ToString();                 }             }             return sTemp;         }         static void Main(string[] args)         {             List<int> listInts = new List<int>();             listInts.Add(1);             listInts.Add(2);             listInts.Add(3);              Console.WriteLine('Object1: {0}', DumpObj(listInts));             int i = 90;              Console.WriteLine('Object2 {0}', DumpObj(i));               List<string> listStrings = new List<string>();             listStrings.Add('1');             listStrings.Add('2');             listStrings.Add('3');              Console.WriteLine('Object3: {0}', DumpObj(listStrings));             Console.ReadKey();         }     } } 

The above code works but I know its an ugly way to achieve this. I wanted to ask from community how can I have this function like –

    public static string DumpObj<T>(object obj)     {         string sTemp = String.Empty;          List<T> list = obj as List<T>;         if (list != null)         {             foreach (T i in list)                 sTemp += i.ToString() + ',';             sTemp.Trim(',');         }         return sTemp;     } 

This gives me compilation errors as I have to specify T while calling DumpObj with error as –

Error 1 The type arguments for method ‘Generic_Collection_Code.Program.DumpObj(object)’ cannot be inferred from the usage. Try specifying the type arguments explicitly. D:\DotNet\Generic_Collection_Code\Generic_Collection_Code\Program.cs 57 47 Generic_Collection_Code

as you can see, obj is an object, i dont know its type while calling dumobj.

I hope I have made myself clear on this one.

I appreciate your time!

Regards Amit

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T11:48:53+00:00Added an answer on May 11, 2026 at 11:48 am

    What is the compilation error you’re getting? If T is declared as a generic type parameter in your context then then the only compile-time issue I can see with that statement is the use of the keyword object as a variable name. At any rate, I’d suggest something like this as best expressing your intention:

    IEnumerable enumerable = obj as IEnumerable;  if (enumerable != null) {     foreach (object item in enumerable)     {         sTemp += item.ToString();     } } 

    You may also want to consider using a StringBuilder if your list is likely to have a lot of items.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have requirement to get Facebook friends list with their images. How can I
The requirement is following: I have to get the location field from page. var
Hi i have following requirement. 1) A website will send a get request with
I have the following weird requirement. I am given: A list of some method
I have a requirement where I can use JS Fiddle dropdown to populate States
I have a requirement to add a standard drop-down from which you can select
Can i add two header text in Datagrid? My Requirement is to have two
Can someone recommend a hosted solution that answers the following requirements (I have seen
I have a requirement where users should be able to navigate from the first
I have the following requirement for creating a user profile in my application: User

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.