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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:54:58+00:00 2026-06-06T12:54:58+00:00

I was trying to write a piece of code for converting a list of

  • 0

I was trying to write a piece of code for converting a list of strings to a list of ints
I got the List<int> list = listOfStr.ConvertAll<int>(delegate(string s) { return ConvertStringToInt(s); }); line from ConvertAll .

    public static List<int> ConvertListOfStringToListOfInt(List<string> listOfStr)
    {
        List<int> list = listOfStr.ConvertAll<int>(delegate(string s) { return ConvertStringToInt(s); });

        return list;
    }
    /// <summary>
    /// converts the given str to integer
    /// </summary>
    /// <param name="str">string to be converted</param>
    /// <returns>returns the int value of the given string</returns>
    public static int ConvertStringToInt(string str)
    {
        int convertedValue = int.Parse(str);

        //(int.TryParse(str, out convertedValue))
        return convertedValue;
    }

The code is working fine except one thing.

I created Unit Test for the above method, the TestMethod is below

        /// <summary>
        ///A test for ConvertListOfStringToListOfInt
        ///</summary>
        [TestMethod()]
        public void ConvertListOfStringToListOfIntTest()
        {
            List<string> listOfStr = new List<string> { "1", "2", "3"}; // TODO: Initialize to an appropriate value
            List<int> expected = new List<int> { 1, 2, 3}; // TODO: Initialize to an appropriate value
            List<int> actual;
            actual = Conversions.ConvertListOfStringToListOfInt(listOfStr);
            Assert.AreEqual(expected, actual);
            //Assert.Inconclusive("Verify the correctness of this test method.");
        }

I have given the same values into the list I pass the method and expected list, just different by type(expected is a list of integer and passed list is a list of strings). I run the test and got this Error Message:

Assert.AreEqual failed. Expected:<System.Collections.Generic.List`1[System.Int32]>. Actual:<System.Collections.Generic.List`1[System.Int32]>.

Well, the types are actually equal so I thought there might be something different inside the lists and I debugged it.

It turned out listOfStr.Capacity is 4 and has null item as the [3] item in it’s items member, for same place in items member expected has a 0 and expected.Capacity is 4,too.

However, actual.Capacity is 3 and it actually does have 3 items in it’s items member.

I tried to newing actual before filling it and using list.Add() for adding values to expected and newing list in the ConvertListOfStringToListOfInt method. But the capacities are still the same. I do not want to set the capacity manually because this method will not be used for lists which has determined capaties.

What can I do to pass this test?
Why is the capacities different?
And how is the capacity of lists are determined, what do they depend on?

This is .NET Framework 4.0.

  • 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-06-06T12:54:59+00:00Added an answer on June 6, 2026 at 12:54 pm

    Try this:

    Assert.IsTrue(actual.SequenceEqual(expected));
    

    The reason being is that lists are complex objects and equality for lists is not determined by each element being the same, rather it is defined as are they pointing to the same list. The sequence equal method iterates the lists for you and determines equality at the item level.

    If the items of the list were complex types, then it would use the default equality comparer for those items.

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

Sidebar

Related Questions

I'm trying to write a piece of code which receives a string, uses the
Trying to write a piece of code to compare two strings. If either are
Hi I'm trying to write a piece of code for a simple verification method
Begginer's question. I'm trying to write small piece of code in Visual Studio (VB.net)
I'm trying to write a little piece of code to show in a portlet
I'm trying to write a unit test for a piece of code that generates
I'm trying to write a piece of code that reads a file line by
Am trying to write a piece of code which will allows the user to
I'm trying to write a piece of code that find shortest path in an
I am trying to write a piece of code for fun using C++ templates.

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.