views:

172

answers:

8

Does .net CLR typecast the objects to the ones mentioned in the collection declaration? If i declare a

List<string> lststrs= new List<string>();
lststrs.add("ssdfsf");

does .net typecasts this object while adding and retriving?????

Well i think the question itself was not clearly understood by everyone.Let me elaborate. In java there is generics ,but if you decompile the code you will notice that ,the compiler places a typecast everywhere the Collection object is used. For Ex: List listOfStrings; listOfStrings.add(""); String value = listOfStrings.get(1); After decompiling the class file we see this List listOfStrings; listOfStrings.add(""); String value = (String)listOfStrings.get(1); Here the compiler has palced the typecast for string.

Now my question is whether it is same in .Net??

+5  A: 

Nope, your list is strongly typed to strings here. That is the entire advantage of using generics.

David Hedlund
+1  A: 

No. It doesn't need to, since the collection is typed.

Rik
+1  A: 

No typecasting is needed at all. The fact of using generics makes your list to be strongly typed automatically.

Konamiman
+3  A: 

It doesn't typecast no.

Generics force type saftey by only allowing objects of the specified type to be added into the generic collection. These are compiler warnings.

In you example, if you were to attempt to add another object, such as a integer, then you would get a compiler warning.

Alastair Pitts
+2  A: 

There's no such thing as a "List" :)

However, there's a List<string>. Here it wont cast your string, as it's a collection of strings.

Consider this, though:

List<object> objects = new List<object>();
objects.Add("Hello SO.com");
string myString = objects.First(); //Wont compile, as objects return an object.
//You need to cast it to a string (for example, by using the as operator).
cwap
+2  A: 

Do you mean this?

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

List is a generic type - if you declare like below you'll get a compialtion error:

List myList = new List(); //<-- big mama of a compilation error

Anyway - since it's a generic List it is strongly typed, so you won't be able to pass-in anything that's not a string (if you do so it will result - again - in a big mama of a compilation error).

JohnIdol
+1  A: 

No it doesn't. As opposed to Java, die CLR implements generics as differnt types on their own, and not via type erasure. So, a List<string> is a different type from List<int>, and both types are contructed dynamically at runtime when needed.

Maximilian Mayerl
+1  A: 

NO.

See the implementation of List.Add:

private T[] _items;

public void Add(T item)
{
    if (this._size == this._items.Length)
    {
        this.EnsureCapacity(this._size + 1);
    }
    this._items[this._size++] = item;
    this._version++;
}
serhio
Well i think the question itself was not clearly understood by everyone.Let me elaborate. In java there is generics ,but if you decompile the code you will notice that ,the compiler places a typecast everywhere the Collection object is used. For Ex: List<String> listOfStrings; listOfStrings.add(""); String value = listOfStrings.get(1); After decompiling the class file we see this List listOfStrings; listOfStrings.add(""); String value = (String)listOfStrings.get(1); Here the compiler has palced the typecast for string. Now my question is whether it is same in .Net??
Ravisha
So, edit your question if you want to find an answer to your detailed question. :)
serhio