Skip to content

Commit c68ed6c

Browse files
authored
Merge pull request #49162 from wwlpublish/986f49a14e4db0975577ec6a78b8b4ce1fddee2df81edbc6f3e1f6901d8ab3f9-live
Modules/M02-apply-interfaces-code
2 parents a65b81c + 35795c2 commit c68ed6c

15 files changed

+526
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.apply-interfaces-code.introduction
3+
title: Introduction
4+
metadata:
5+
title: Introduction
6+
description: "Introduction to interface application in C# code."
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 2
12+
content: |
13+
[!include[](includes/1-introduction.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.apply-interfaces-code.explore-default-interface-implementation
3+
title: Explore default interface implementation
4+
metadata:
5+
title: Explore default interface implementation
6+
description: "Explore default interface implementation in C# code."
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 4
12+
content: |
13+
[!include[](includes/2-explore-default-interface-implementation.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.apply-interfaces-code.define-method-interface-parameter
3+
title: Define a method with an interface as a parameter
4+
metadata:
5+
title: Define a method with an interface as a parameter
6+
description: "Define a method with an interface as parameter in C# code."
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 4
12+
content: |
13+
[!include[](includes/3-define-method-interface-parameter.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.apply-interfaces-code.learn-system-defined-interfaces
3+
title: Learn to use system defined interfaces
4+
metadata:
5+
title: Learn to use system defined interfaces
6+
description: "Learn to use system defined interfaces in C# code."
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 6
12+
content: |
13+
[!include[](includes/4-learn-system-defined-interfaces.md)]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.apply-interfaces-code.exercise-create-flexible-code-with-interfaces
3+
title: 'Exercise: Create flexible code with interfaces'
4+
metadata:
5+
title: 'Exercise: Create flexible code with interfaces'
6+
description: "Exercise: Create flexible code with interfaces in C# code."
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 20
12+
content: |
13+
[!include[](includes/5-exercise-create-flexible-code-with-interfaces.md)]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.apply-interfaces-code.knowledge-check
3+
title: Knowledge check
4+
metadata:
5+
title: Knowledge check
6+
description: "Knowledge check"
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 2
12+
quiz:
13+
title: "Check your knowledge"
14+
questions:
15+
- content: "What is a key benefit of using default implementations in interfaces?"
16+
choices:
17+
- content: "They allow for code reuse across different implementations."
18+
isCorrect: true
19+
explanation: "Correct. Default implementations enable sharing common functions across different classes without copying code, promoting code reuse."
20+
- content: "They automatically apply to all classes without any need for implementation."
21+
isCorrect: false
22+
explanation: "Incorrect. Classes must still implement the interface and can choose to use or override the default methods."
23+
- content: "They prevent any changes to the interface once defined."
24+
isCorrect: false
25+
explanation: "Incorrect. Default implementations actually allow adding new methods to interfaces without breaking existing implementations."
26+
- content: "What is the purpose of the IDisposable interface in C#?"
27+
choices:
28+
- content: "To release unmanaged resources."
29+
isCorrect: true
30+
explanation: "Correct. The IDisposable interface is used to release unmanaged resources, such as file handles and network connections."
31+
- content: "To compare objects."
32+
isCorrect: false
33+
explanation: "Incorrect. The IComparable interface is used for comparing objects, not releasing unmanaged resources."
34+
- content: "To iterate over collections."
35+
isCorrect: false
36+
explanation: "Incorrect. The IEnumerable interface is used for iterating over collections, not releasing unmanaged resources."
37+
- content: "What is the main advantage of using system-defined interfaces like IEnumerable and IDisposable in C#?"
38+
choices:
39+
- content: "They provide standard methods that ensure consistent behavior across different types."
40+
isCorrect: true
41+
explanation: "Correct. System-defined interfaces provide standard methods that can be implemented across different types, ensuring uniformity and predictability in behavior."
42+
- content: "They allow for the automatic generation of code."
43+
isCorrect: false
44+
explanation: "Incorrect. System-defined interfaces do not automatically generate code; they provide standard methods for consistent behavior."
45+
- content: "They prevent the need for any implementation in classes."
46+
isCorrect: false
47+
explanation: "Incorrect. Classes implementing system-defined interfaces must still provide implementations for the interface methods."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.apply-interfaces-code.summary
3+
title: Summary
4+
metadata:
5+
title: Summary
6+
description: "Summary"
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 2
12+
content: |
13+
[!include[](includes/7-summary.md)]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Default implementations in interfaces enable you to add new methods directly into an interface and provide a default implementation. A class that uses this interface can either use the default method or override and provide its own implementation.
2+
3+
Imagine you're a software developer at a tech company building a home automation system. Your task is to design and implement various types of lights with different functionalities using interfaces in C#. Using interfaces allows the system to be flexible and easily extendable as new types of lights and features are added.
4+
5+
After completing this module, you’ll be able to:
6+
7+
- Define and implement basic interfaces in C#.
8+
- Implement default methods in interfaces.
9+
- Use interfaces as method parameters to promote loose coupling.
10+
- Implement and use common system-defined interfaces like IEnumerable, IDisposable, and IComparable.
11+
- Override default implementations in specific classes.
12+
13+
## Prerequisites
14+
15+
- Visual Studio Code installed with the C# Dev Kit.
16+
- Basic knowledge of the Visual Studio Code IDE.
17+
- Basic understanding of the C# programming language.
18+
- Able to create and implement basic interfaces.
19+
- Familiar with classes, abstract classes, and inheritance.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
**Default interface implementations** enable you to add new methods directly into an interface and provide a default implementation. A class that uses this interface, can either use the default method, or it can override and provide its own implementation.
2+
3+
## Default implementations
4+
5+
Default implementations in interfaces let you add new methods to an interface without causing problems.
6+
7+
- An interface can provide a body for its methods using default implementations.
8+
- Classes using the interface don't need to write their own versions of these methods.
9+
- This feature helps when you want to change the interface without breaking anything.
10+
11+
> [!NOTE]
12+
> Even with default implementations, a class doesn't automatically get the interface's methods. To use a method from the interface, the variable must be of the interface type.
13+
14+
## Benefits
15+
16+
Default implementations in interfaces have several benefits:
17+
18+
- **Code reuse**: You can share common functions across different implementations without copying code.
19+
- **Backward compatibility**: You can add new methods to interfaces without changing all classes using the interface to prevents breaking changes.
20+
- **Flexibility**: Classes using the interface can change the default implementation if they need different behavior.
21+
22+
For example, let's say you're working with a customer management system and you add a new method to apply a loyalty discount in an existing interface. The default method calculates a basic discount. But if a "VIP customer" class needs a different calculation, it can modify this method.
23+
24+
## Define default implementations
25+
26+
The syntax for defining default implementations in interfaces extends the interface declaration to include method bodies. The following example defines a default implementation in an interface based on a customer management system scenario:
27+
28+
```csharp
29+
interface ICustomer
30+
{
31+
int CustomerId { get; set; } // No default implementation
32+
33+
void ApplyLoyaltyDiscount()
34+
{
35+
// default implementation
36+
Console.WriteLine("Applying 10% loyalty discount.");
37+
}
38+
}
39+
```
40+
41+
In this example, the `ApplyLoyaltyDiscount` method has a default implementation within the `ICustomer` interface. The `CustomerId` property is also defined in the interface but doesn't have a default implementation.
42+
43+
> [!NOTE]
44+
> From C# 8.0 onwards, interfaces can include default method implementations. The change allows new methods to be added to interfaces without breaking existing code. Classes can use these defaults or provide their own implementations.
45+
46+
Next, implement a "VIPCustomer" class that needs a different discount calculation. This class can implement the `ICustomer` interface, provide its own implementation for the `CustomerId` property, and override the `ApplyLoyaltyDiscount` method like so:
47+
48+
```csharp
49+
class VIPCustomer : ICustomer
50+
{
51+
public int CustomerId { get; set; } // Implementing property from interface
52+
53+
void ICustomer.ApplyLoyaltyDiscount()
54+
{
55+
// override default implementation for VIP customers
56+
Console.WriteLine($"Applying 20% loyalty discount for VIP customer with ID {CustomerId}.");
57+
}
58+
}
59+
```
60+
61+
In this case, the `VIPCustomer` class applies a 20% discount instead of the default 10%. The `CustomerId` is used in the message when applying the discount.
62+
63+
Default implementations in interfaces allow for the addition of new methods with the method implementation provided in the interface. The default implementations promote code reuse, backward compatibility, and flexibility. It's a practical tool in C# Object-Oriented Programming that enhances adaptability without disrupting existing code.
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
In C#, interfaces establish a contract for classes. In this unit, you explore how methods accept interfaces as parameters, learn the syntax for defining these methods, and review a practical sample implementation.
2+
3+
## Methods that accept an interface
4+
5+
Your methods in C# can accept interfaces as parameters to ensure that the passed objects adhere to specific contracts. Using interfaces as parameters allows for more flexible and reusable code.
6+
7+
For instance, consider a method that accepts an `IAnimal` interface as a parameter:
8+
9+
```csharp
10+
public interface IAnimal
11+
{
12+
void MakeSound();
13+
}
14+
15+
public void MakeAnimalSound(IAnimal animal)
16+
{
17+
animal.MakeSound();
18+
}
19+
```
20+
21+
In this example, the `MakeAnimalSound` method accepts an `IAnimal` interface, ensuring that any object passed to it has a `MakeSound` method.
22+
23+
> [!NOTE]
24+
> Using interfaces as method parameters promotes loose coupling and enhances testability.
25+
26+
## Example: interfaces as method parameters
27+
28+
In the following example, we have several files each containing a different class or interface. It's common to place items in separate files for better organization and readability.
29+
30+
### `IAnimal.cs`
31+
32+
The `IAnimal.cs` file defines the `IAnimal` interface, which includes a method `MakeSound`.
33+
34+
```csharp
35+
public interface IAnimal
36+
{
37+
void MakeSound();
38+
}
39+
```
40+
41+
### `Dog.cs` and `Cat.cs`
42+
43+
These files contain the `Dog` and `Cat` classes, which implement the `IAnimal` interface and provide the `MakeSound` method.
44+
45+
```csharp
46+
public class Dog : IAnimal
47+
{
48+
public void MakeSound()
49+
{
50+
Console.WriteLine("Woof!");
51+
}
52+
}
53+
54+
public class Cat : IAnimal
55+
{
56+
public void MakeSound()
57+
{
58+
Console.WriteLine("Meow!");
59+
}
60+
}
61+
```
62+
63+
### `AnimalSound.cs`
64+
65+
This file contains the `AnimalSound` class, which has a method `MakeAnimalSound` that accepts an `IAnimal` interface as a parameter and calls the `MakeSound` method.
66+
67+
```csharp
68+
public class AnimalSound
69+
{
70+
public void MakeAnimalSound(IAnimal animal)
71+
{
72+
animal.MakeSound();
73+
}
74+
}
75+
```
76+
77+
### `Program.cs`
78+
79+
This file contains the `Program` class with the `Main` method, which creates instances of `Dog` and `Cat`, and uses the `AnimalSound` class to make them produce sounds.
80+
81+
```csharp
82+
public class Program
83+
{
84+
public static void Main()
85+
{
86+
IAnimal dog = new Dog();
87+
IAnimal cat = new Cat();
88+
89+
AnimalSound animalSound = new AnimalSound();
90+
animalSound.MakeAnimalSound(dog); // Output: Woof!
91+
animalSound.MakeAnimalSound(cat); // Output: Meow!
92+
}
93+
}
94+
```
95+
96+
This example demonstrates how interfaces can be used as method parameters, promoting loose coupling and enhancing testability. It's a simple yet effective way to understand the concept of interfaces in C#.

0 commit comments

Comments
 (0)