Is there a cost in passing an object to a function that implements a particular interface where the function only accepts that interface? Like:
Change (IEnumerable<T> collection)
and I pass:
List<T>
LinkedList<T>
CustomCollection<T>
which all of them implements IEnumerable. But when you pass any of those to the Change method, are they cast to IEnumerable, thus there is a cast cost but also the issue of losing their unique methods, etc?
Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.
C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).
Bahasa pemrograman C ini dikembangkan antara tahun 1969 – 1972 oleh Dennis Ritchie. Yang kemudian dipakai untuk menulis ulang sistem operasi UNIX. Selain untuk mengembangkan UNIX, bahasa C juga dirilis sebagai bahasa pemrograman umum.
No, there is no cast involved since List<T>
IS-A
IEnumerable<T>
. This is using polymorphism which does not require casting.
Edit: Here is an example:
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
foo(new List<int>());
}
static void foo(IEnumerable<int> list) { }
}
The IL for Main
is:
.method private hidebysig static void Main() cil managed
{
.entrypoint
.maxstack 8
L_0000: nop
L_0001: newobj instance void [mscorlib]System.Collections.Generic.List`1<int32>::.ctor()
L_0006: call void Program::foo(class [mscorlib]System.Collections.Generic.IEnumerable`1<int32>)
L_000b: nop
L_000c: ret
}
And as you can see there is no casting involved. The instance of List<T>
is pushed onto the stack and them foo
is called immediately after.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With