2

I'm wondering how one can prove what the .Net framework is doing behind the scenes.

I have a method that accepts a parameter of a List<String> originalParameterList.

In my method I have another List<String> newListObj if I do the following:

List<String> newListObj = originalParameterList
    newListObj.Add(value);
    newListObj.Add(value1);
    newListObj.Add(value2);

The count of the originalParameterList grows (+3).

If I do this:

List<String> newListObj = new List<String>(originalParamterList);

newListObj.Add(value);
newListObj.Add(value1);
newListObj.Add(value2);

The count of the originalParameterList stays the sames (+0).

I also found that this code behaves the same:

List<String> newListObj = new List<String>(originalParamterList.ToArray());

newListObj.Add(value);
newListObj.Add(value1);
newListObj.Add(value2);

The count of the originalParameterList stays the sames (+0).

My question is, is there a way to see what the .Net Framework is doing behind the scenes in a definitive way?

webdad3
  • 8,893
  • 30
  • 121
  • 223
  • You can examine the IL. Also, you are passing references to the new List when you create it. You aren't affecting the original list. – Mike Cheel Jun 28 '13 at 15:45
  • Read [**MSDN**](http://msdn.microsoft.com/en-us/library/fkbw11z0.aspx) carefully. You can also use a reflector, for example [`ILSpy`](http://ilspy.net/). Another resource is the [C# language specification](http://www.ecma-international.org/publications/standards/Ecma-334.htm). – Tim Schmelter Jun 28 '13 at 15:45
  • see [http://stackoverflow.com/questions/186891/why-use-ref-keyword-when-passing-an-object](http://stackoverflow.com/questions/186891/why-use-ref-keyword-when-passing-an-object) – saamorim Jun 28 '13 at 15:46
  • It is particularly clear that you are getting a new object here since you use the 'new' keyword. – Gray Jun 28 '13 at 15:47
  • You can just examine the syntax and determine what it is doing. A new will create a new object. And = will create a reference. – paparazzo Jun 28 '13 at 15:51

7 Answers7

1

You can load your assembly into ILDASM and(when loaded),find your method and double-click it, it will show the cil code of that method.Just type "IL" in windows start menu in the search.

Alternatively you can you can use these following ways to also create a new independent list

        private void GetList(List<string> lst)
        {
            List<string> NewList = lst.Cast<string>().ToList();
            NewList.Add("6");
            //not same values.
            //or....
            List<string> NewList = lst.ConvertAll(s => s);
            NewList.Add("6");
            //again different values

        }
terrybozzio
  • 4,424
  • 1
  • 19
  • 25
0

Normally, the documentation should give enough information to use the API.

In your specific example, the documentation for public List(IEnumerable<T> collection) says (emphasis mine):

Initializes a new instance of the List class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

For the reference here is the source code for the constructor:

public List (IEnumerable <T> collection)
{
    if (collection == null)
        throw new ArgumentNullException ("collection");

    // initialize to needed size (if determinable)
    ICollection <T> c = collection as ICollection <T>;
    if (c == null) {
        _items = EmptyArray<T>.Value;;
        AddEnumerable (collection);
    } else {
        _size = c.Count;
        _items = new T [Math.Max (_size, DefaultCapacity)];
        c.CopyTo (_items, 0);
    }
}

void AddEnumerable (IEnumerable <T> enumerable)
{
    foreach (T t in enumerable)
    {
        Add (t);
    }
}
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
0

The simplest way to do it is simply go to MSDN

http://msdn.microsoft.com/en-us/library/fkbw11z0.aspx

It says that

Initializes a new instance of the List class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

so internally it`s simply add all elements of passed IEnumerable into new list. It also says that

this is a O(n) operation

which means that no optimizations assumed.

Oleh Nechytailo
  • 2,155
  • 17
  • 26
0

That's because the frist case you referenced the original list (since it is a reference type), and you modified it's collection via newListObj. The second and third case you copied the original objects' collection via List constructor List Class, and you modified the new collection, which is not take any effect to the original.

speti43
  • 2,886
  • 1
  • 20
  • 23
0

As others already said, there are various tools that let you examine the source code of the .NET framework. I personally prefer dotPeek from JetBrains, which is free.

In the specific case that you have mentioned, I think when you pass a list into the constructor of another list, that list is copied. If you just assign one variable to another, those variables are then simply referring to the same list.

mario
  • 1,248
  • 9
  • 9
0

You can either

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108
0

This is the code from List constrcutor:

public List(IEnumerable<T> collection)
{
    if (collection == null)
    {
        ThrowHelper.ThrowArgumentNullException(ExceptionArgument.collection);
    }
    ICollection<T> collection2 = collection as ICollection<T>;
    if (collection2 != null)
    {
        int count = collection2.Count;
        this._items = new T[count];
        collection2.CopyTo(this._items, 0);
        this._size = count;
        return;
    }
    this._size = 0;
    this._items = new T[4];
    using (IEnumerator<T> enumerator = collection.GetEnumerator())
    {
        while (enumerator.MoveNext())
        {
            this.Add(enumerator.Current);
        }
    }
}

As you can see when you calls costructor which takes IEnumerable it copies all data to itself.

Alexandr Mihalciuc
  • 2,537
  • 15
  • 12