C# net 3.50 - List<T>.Count - bug ?

K

kk

Personally I do not like the "sophisticated" code but,
anyway I have this (I squeez it to save a space):

using System;
using System.Collections.Generic;


public class SamplesArray {
public static void Main() {

// Creates and initializes a new integer array and a new Object array.
List<int> myIntList = new List<int>();

myIntList.AddRange (new int[5] { 1, 2, 3, 4, 5 } );

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

myStrList.AddRange (new string[2] { "aa","bb"} );

myStrList.Add("cc");

Console.WriteLine( "0:" + myStrList[0] );
Console.WriteLine( "2:" + myStrList[2] );

Console.WriteLine( "string List1: " );
PrintValues( myStrList );
Console.WriteLine();

myStrList.Clear();

myStrList.AddRange (new string[2] { "zz","yy"} );

Console.WriteLine( "0:" + myStrList[0] );
Console.WriteLine( "1:" + myStrList[1] );

Console.WriteLine( "string List2: " );
//PrintValues( myStrList );
Console.WriteLine();

Console.WriteLine( "integer array:" );
PrintValues( myIntList );

Console.WriteLine( "integer array size {0} [{1}]" , @"count:",
myIntList.Count );
}


public static void PrintValues( List<int> myList ) {
foreach ( int i in myList ) {
Console.Write( "\t{0}", i );
}
Console.WriteLine();
}


public static void PrintValues( List<string> myList ) {
foreach ( string i in myList ) {
Console.Write( "\t{0}\n", i );
}
Console.WriteLine();
}

}

gives:

Setting environment for using Microsoft Visual Studio 2008 x86 tools.
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

0:aa
2:cc
string List1:
aa
bb
cc


0:zz
1:yy
string List2:

integer array:
1 2 3 4 5
integer array size count: [5]



AFTER uncommenting the //PrintValues( myStrList );

it gives:
Setting environment for using Microsoft Visual Studio 2008 x86 tools.
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

0:aa
2:cc
string List1:
aa
bb
cc


0:zz
1:yy
string List2:
zz
yy


integer array:
1 2 3 4 5
integer array size count: [

WHERE is "5"

is it my mistake :) or BUG?
 
K

kk

Did you get count 5 in both cases?

I have all updates applied - have the same result (BAD) in net 2.00 and 3.5

(You have to uncomment the line i mention!)

--
kk


Andrew Faust said:
This code worked fine for me using VS 2008 & .Net 3.5.

Andrew Faust

kk said:
Personally I do not like the "sophisticated" code but,
anyway I have this (I squeez it to save a space):

using System;
using System.Collections.Generic;


public class SamplesArray {
public static void Main() {

// Creates and initializes a new integer array and a new Object
array.
List<int> myIntList = new List<int>();

myIntList.AddRange (new int[5] { 1, 2, 3, 4, 5 } );

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

myStrList.AddRange (new string[2] { "aa","bb"} );

myStrList.Add("cc");

Console.WriteLine( "0:" + myStrList[0] );
Console.WriteLine( "2:" + myStrList[2] );

Console.WriteLine( "string List1: " );
PrintValues( myStrList );
Console.WriteLine();

myStrList.Clear();

myStrList.AddRange (new string[2] { "zz","yy"} );

Console.WriteLine( "0:" + myStrList[0] );
Console.WriteLine( "1:" + myStrList[1] );

Console.WriteLine( "string List2: " );
//PrintValues( myStrList );
Console.WriteLine();

Console.WriteLine( "integer array:" );
PrintValues( myIntList );

Console.WriteLine( "integer array size {0} [{1}]" , @"count:",
myIntList.Count );
}


public static void PrintValues( List<int> myList ) {
foreach ( int i in myList ) {
Console.Write( "\t{0}", i );
}
Console.WriteLine();
}


public static void PrintValues( List<string> myList ) {
foreach ( string i in myList ) {
Console.Write( "\t{0}\n", i );
}
Console.WriteLine();
}

}

gives:

Setting environment for using Microsoft Visual Studio 2008 x86 tools.
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

0:aa
2:cc
string List1:
aa
bb
cc


0:zz
1:yy
string List2:

integer array:
1 2 3 4 5
integer array size count: [5]



AFTER uncommenting the //PrintValues( myStrList );

it gives:
Setting environment for using Microsoft Visual Studio 2008 x86 tools.
Microsoft (R) Visual C# 2008 Compiler version 3.5.21022.8
for Microsoft (R) .NET Framework version 3.5
Copyright (C) Microsoft Corporation. All rights reserved.

0:aa
2:cc
string List1:
aa
bb
cc


0:zz
1:yy
string List2:
zz
yy


integer array:
1 2 3 4 5
integer array size count: [

WHERE is "5"

is it my mistake :) or BUG?
 
P

Pavel Minaev

Did you get count 5 in both cases?

I have all updates applied - have the same result (BAD) in net 2.00 and 3..5

(You have to uncomment the line i mention!)

Works fine here with uncommented line, though I have 3.5 SP1 beta.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top