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 7278319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:51:30+00:00 2026-05-28T22:51:30+00:00

I have written a method which is public List<List<object>> Fetch(string data) , inside I

  • 0

I have written a method which is
public List<List<object>> Fetch(string data),
inside I create
List<List<object>> p = new List<List<object>>();

my boss now wants to return a IList<IList<object>> instead of List<List<object>> ie
public IList<IList<object>> Fetch(string data),

so when I try do
return (IList<IList<object>>) p; //throws an exception

How do I convert
List<List<object>> to IList<IList<object>> and back to List<List<object>>

  • 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. Editorial Team
    Editorial Team
    2026-05-28T22:51:31+00:00Added an answer on May 28, 2026 at 10:51 pm

    You can’t perform that conversion via straight casting – it wouldn’t be safe. Instead, you should use:

    IList<IList<object>> ret = new List<IList<object>>();
    

    Then for each “sublist” you can use:

    // Or whatever
    ret.Add(new List<object>());
    

    Finally, just return ret.

    You could use LINQ to perform the conversion of your existing List<List<object>> when you return it – but it would be better to just create a more appropriate type to start with, as shown above.


    To understand why some of the existing answers are wrong, suppose you could do this:

    IList<IList<object>> p = new List<List<object>>();
    

    Then this would be valid:

    List<List<object>> listOfLists = new List<List<object>>();
    IList<IList<object>> p = listOfLists;
    p.Add(new object[]);
    List<object> list = p[0];
    

    But p[0] is a reference to an object[], not a List<object>… our supposedly type-safe code doesn’t look as safe any more…

    Fortunately, IList<T> is invariant to prevent exactly this problem.

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

Sidebar

Related Questions

I have written method which should return string with data from select query, but
I have written a small webservice[Axis2/Java] which exposes a method public String Fill(String cacheName
I have written a method which writes an object to file. I used generics
I have written an extension method for string manipulation. I'm confused what should I
I have class1.m . I declared a method and written in it. Now i
I have a large project which was originally written in C#1.1 and is now
I have written a boolean method which can be called upon to read information
I have written a helper class which uses the Action - delegate as method
I need a method written in Ruby which computes Variations. I have already written
Ok, so basically I need to create a generic method which takes a list

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.