Jūs esate

Using interfaces in C#

Simple example with interfaces. This example just shows, that when we have interface (method's signatures, delegates etc.) we don't care, how this interface is implemented. We have interface and class: interface IList { void Add(object o); void Remove(object o); void Print(); } class Implementation : IList { private ArrayList arrayList = new ArrayList(); public void Add(object o) { arrayList.Add(o); } public void Remove(object o) { arrayList.Remove(o); } public void Print() { foreach (string line in arrayList) Console.WriteLine(line); } } Somewhere in the code we have methods: void Foo(IList list) { list.Add("I"); list.Add("am"); list.Add("using"); list.Add("interfaces"); } void Foo2(IList list) { list.Print(); } And we call them: IList list = new Implementation(); Foo(list); Foo2(list); Try to guess, what is the output? ;)

Komentarai

I
am
using
interfaces

Yep ;)

Komentuoti