Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
1.3 Basics: interfaces
//1.3 Interfaces using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { //interface is a declaration of a contract, i.e. if interface does A() //and I give you an object that implements this interface //then this object must also do A() //This way you can write code that takes not concrete types, but interfaces //And then supply it any type that satisfies this interface ConcertArena concert = new ConcertArena(); concert.PerformConcert(new List<ISinging>(){new RobbieWilliams(), new SnoopLion()}); //concert.PerformConcert(new List<ISinging>(){new JamesBond()}); //JamesBond can't sing } } public class ConcertArena { public void PerformConcert(List<ISinging> artists) { foreach(ISinging a in artists) { a.Sing(); } } } public interface ISinging { void Sing(); } public class RobbieWilliams : ISinging { public void Sing() { Console.WriteLine("♪♪♪ robbie williams sings ♪♪♪"); } } public class SnoopLion : ISinging { public void Sing() { Console.WriteLine("♪♪♪ snoop lion sings ♪♪♪"); } } public class JamesBond { public void Sing() { Console.WriteLine("Daniel Craig tries to sing, but well ..."); } } }
run
|
edit
|
history
|
help
0
Recursion
edmond@DESKTOP-M2BC2LH
Encapsulation and abstration
C# Abstract Syntax Tree Example: Compiler for a numeric expression
get weekofDay from DateTime in C#
dfg
addr.sin_family == AF_UNSPEC
testing_editor
dgd dy retert
Generic Reference constraint