Jūs esateŽurnalai / Ernestas Kardzys's blog / Using interfaces in C#

Using interfaces in C#


ParašėErnestas Kardzys - 2009 Balandžio 23

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? ;)

Yep ;)

I
am
using
interfaces

Skelbti naują komentarą

Šio laukelio turinys bus laikomas privatus ir nerodomas viešai.
  • Web puslapiu adresai ir el. pašto adresai automatiškai tampa nuorodomis.
  • Leidžiamos HTML žymės: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Linijos ir paragrafai atskiriami automatiškai
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].

Daugiau informacijos apie teksto formatavimą

CAPTCHA
Šis klausimas yra skirtas įsitikinti, jog jūs esate žmogus, ir sustabdyti automatinį šlamšto siuntimą.
3 + 0 =
Išspręskite šią paprastą matematinę užduotį ir įveskite atsakymą. Pvz., jei užduotis yra 1+3, įveskite 4.