| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  | title: Interface | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | An interface is similar to a class or struct but without implementation for its members. | 
					
						
							|  |  |  | An interface declares a contract or a behavior that implementing classes should have. | 
					
						
							|  |  |  | It may declare only properties, methods and events with NO access modifiers. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | All the declared members must be implemented in the inharit class, otherwise will have an compile error. | 
					
						
							|  |  |  | as a convention we will mark interface with the letter I at the begenning (IMyInterface || IUserOptions). | 
					
						
							|  |  |  | You define an interface by using the interface keyword. | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | All members of an interface are: | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  | implicitly abstract, | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | implicitly public, cannot declare an access modifier such as protected, internal private etc... | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  | An Interface can: | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | * Inherit from other interfaces. | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  | * Inherit from multiple interfaces at the same time | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  | * Contain only methods, properties, events, and indexers. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | An Interface can not : | 
					
						
							|  |  |  | * Inherit from a class. | 
					
						
							|  |  |  | * Have implementation. | 
					
						
							|  |  |  | * Have access modifiers other than public. | 
					
						
							|  |  |  | * Be instantiated. | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Using interfaces allows us to change our implementation in our project without breaking other parts, | 
					
						
							|  |  |  | and only have to change the one place where the object is created. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Interface Example: | 
					
						
							|  |  |  | ```csharp | 
					
						
							|  |  |  | public Interface IUserFavoriteFood | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   void AddFood(); | 
					
						
							|  |  |  |   Task<User> EatFavoriteFood(int id); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | --- | 
					
						
							|  |  |  | Interface inheritance and implementation: | 
					
						
							|  |  |  | ```csharp | 
					
						
							|  |  |  | public class UserHungry : IUserFavoriteFood | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   public AddFood() | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Implementation: | 
					
						
							|  |  |  |     // A method to add food. | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2018-10-19 13:53:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-12 15:37:13 -04:00
										 |  |  |   public Task<User> EatFavoriteFood(int id) | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     // Implementation: | 
					
						
							|  |  |  |     // A method to Eat food by id. | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` |