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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:55:41+00:00 2026-05-30T05:55:41+00:00

List<authorinfo> aif = new List<authorinfo>(); for (int i = 0; i < 5; i++)

  • 0
List<authorinfo> aif = new List<authorinfo>();
for (int i = 0; i < 5; i++)
{
    aif.Add(null);
}
aif[0] = new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844);
aif[1] = new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972);
aif[2] = new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844);
aif[3] = new authorinfo("Robinson Crusoe", "Daniel", "Defoe", 1719);
aif[4] = new authorinfo("2001: A Space Odyssey", "Arthur", "Clark", 1968); 
//authorinfo ai = new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844); 
foreach (authorinfo i in aif)
{
    Console.WriteLine(i);
}

Okay the thing is, when i remove the for-loop at the top the program wont start, why? Because i need to add null to the list.

I know I’m doing this the wrong way. I just want the aif[0-4] to be there, it doesn’t make sense that i have to add null objects to not get an outofrange error.

Help please?

  • 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-30T05:55:43+00:00Added an answer on May 30, 2026 at 5:55 am

    Just add the new object instances themselves:

      List<authorinfo> aif = new List<authorinfo>();
      aif.Add(new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844));
      //... and so on
    

    Right now you are using null as a placeholder element which you then overwrite using the indexer – you don’t have to do this (nor should you).

    As an alternative and if you know your list elements in advance you could also use the collection initializer:

      List<authorinfo> aif = new List<authorinfo>()
      {
         new authorinfo("The Count of Monte Cristo", "Alexandre", "Dumas", 1844),
         new authorinfo("Rendezvous with Rama", "Arthur", "Clark", 1972),
         new authorinfo("The Three Musketeers", "Alexandre", "Dumas", 1844)
      };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

List<Car> oUpdateCar = new List<Car>(); oUpdateCar.Add(new Car()); oUpdateCar[0].name = Color; oUpdateCar[0].value = red; oUpdateCar.Add(new
List <Customer> collCustList = new List<Customer>(); I tried if(A==B) collCustList.Add(new Customer(99, H, P)); else
List<string> arguments = new List<string>(Environment.GetCommandLineArgs().Skip(1).Take(4)); int variant = consoleOptions.HandleInput(arguments); public int HandleInput(List<string> input) {
List<String> listA = new ArrayList<String>(); listA.add(a); listA.add(b); listA.add(c); listA.add(d); List<String> listB = new ArrayList<String>();
List<int> a = new List<int>{ 1,1,2,2,3,4,5 }; What's the quickest way to do this
List itemsToMove = new List(); foreach (ListViewItem item in lvScanRepository.SelectedItems) { itemsToMove.Add(item); } foreach
List<string> list1 = new List<string>(); list1.Add(Blah); list1.Add(Bleh); list1.Add(Blih); List<string> list2 = new List<string>(); list2.Add(Ooga);
List<Object> myList = new ArrayList<String>(); //(hint: no) Map<Integer> myMap = new HashMap<int>(); // (hint:
List<String> flowers = new ArrayList<String>(); My for loop currently looks like this... for (int
List<decimal> FindSumSubset(decimal sum, List<decimal> list) { for (int i = 0; i < list.Count;

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.