Skip to content

Commit 28eb8f2

Browse files
authored
Merge pull request #49161 from wwlpublish/814a34e28d92b7dc8ab942b681676c074f2e486813bd980a8fdd7a7326dd3a9b-live
Modules/M01-discover-interfaces
2 parents 352e93b + baec64a commit 28eb8f2

17 files changed

+474
-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.discover-interfaces.introduction
3+
title: Introduction
4+
metadata:
5+
title: Introduction
6+
description: "Introduction to C# interfaces."
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.discover-interfaces.understand-interfaces
3+
title: Understand interfaces
4+
metadata:
5+
title: Understand interfaces
6+
description: "Understand interfaces in C# code."
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 3
12+
content: |
13+
[!include[](includes/2-understand-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.discover-interfaces.create-interface-properties
3+
title: Implement interface properties
4+
metadata:
5+
title: Implement interface properties
6+
description: "Implement interface properties 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-create-interface-properties.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.discover-interfaces.create-interface-methods
3+
title: Create interface methods
4+
metadata:
5+
title: Create interface methods
6+
description: "Create interface methods 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/4-create-interface-methods.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.discover-interfaces.understand-value-interfaces
3+
title: Understand the value of interfaces
4+
metadata:
5+
title: Understand the value of interfaces
6+
description: "Understand the value of interfaces in C# code."
7+
ms.date: 02/13/2025
8+
author: wwlpublish
9+
ms.author: eric
10+
ms.topic: unit
11+
durationInMinutes: 5
12+
content: |
13+
[!include[](includes/5-understand-value-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.discover-interfaces.exercise-implement-interfaces-project
3+
title: 'Exercise: Implement interfaces in project'
4+
metadata:
5+
title: 'Exercise: Implement interfaces in project'
6+
description: "Exercise: Implement interfaces in project with C#."
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/6-exercise-implement-interfaces-project.md)]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.wwl.discover-interfaces.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: "Which of the following is true about properties in an interface?"
16+
choices:
17+
- content: "They can have instance data fields."
18+
isCorrect: false
19+
explanation: "Incorrect. Properties in an interface don't declare instance data fields."
20+
- content: "They typically don't have a body."
21+
isCorrect: true
22+
explanation: "Correct. Interface properties typically don't have a body; they only define accessors."
23+
- content: "They must be static."
24+
isCorrect: false
25+
explanation: "Incorrect. Interface properties aren't required to be static."
26+
- content: "You're implementing an interface in a class. What must you ensure?"
27+
choices:
28+
- content: "Provide public, nonstatic implementations for all interface members."
29+
isCorrect: true
30+
explanation: "Correct. When a class implements an interface, it must provide public, nonstatic implementations for all interface members."
31+
- content: "Provide private implementations for all interface members."
32+
isCorrect: false
33+
explanation: "Incorrect. Interface members must be implemented as public, not private."
34+
- content: "Provide static implementations for all interface members."
35+
isCorrect: false
36+
explanation: "Incorrect. Interface members should be implemented as nonstatic."
37+
- content: "In a scenario where you need to implement an interface in a class, what syntax is used?"
38+
choices:
39+
- content: "class ClassName : InterfaceName"
40+
isCorrect: true
41+
explanation: "Correct. The correct syntax to implement an interface in a class is 'class ClassName : InterfaceName'."
42+
- content: "class ClassName implements InterfaceName"
43+
isCorrect: false
44+
explanation: "Incorrect. The 'implements' keyword isn't used in C#; the correct keyword is ':'."
45+
- content: "class ClassName extends InterfaceName"
46+
isCorrect: false
47+
explanation: "Incorrect. The 'extends' keyword isn't used in C#; the correct keyword is ':'."
48+
- content: "What is a key benefit of using interfaces in C# programming?"
49+
choices:
50+
- content: "They allow for better code organization and flexibility."
51+
isCorrect: true
52+
explanation: "Correct. Interfaces provide a way to define contracts for classes, enabling better code organization and flexibility."
53+
- content: "They allow classes to inherit from multiple base classes."
54+
isCorrect: false
55+
explanation: "Incorrect. Interfaces don't allow multiple inheritance of classes; they allow a class to implement multiple interfaces."
56+
- content: "They provide default implementations for all members."
57+
isCorrect: false
58+
explanation: "Incorrect. Interfaces may define default implementations for some members starting with C# 8.0, but this isn't their primary benefit."
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.discover-interfaces.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/8-summary.md)]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Interfaces in C#/.NET programming play a crucial role in defining contracts for classes, enabling polymorphism, and organizing code efficiently.
2+
3+
You're a software developer at a company that is building a new application. Your task is to implement various interfaces in a C# project to ensure that different components of the application can interact seamlessly.
4+
5+
In this module, you learn how to design an interface with properties in .NET, develop a class that implements the interface, and formulate property definitions within the interface.
6+
7+
## Learning objectives
8+
9+
- Design an interface property.
10+
- Design an interface method.
11+
- Implements an interface in a class.
12+
- Understand the value of interfaces.
13+
14+
## Prerequisites
15+
16+
- Visual Studio Code installed with the C# Dev Kit.
17+
- Basic knowledge of the Visual Studio Code IDE.
18+
- Basic understanding of the C# programming language.
19+
- Familiarity with classes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
An interface is like a to-do list or a contract. It outlines what jobs need to be done, without specifying how these jobs should be done. The contract nature of interfaces allows for multiple classes to accomplish the same tasks in their own unique ways.
2+
3+
### Interfaces are like a contract
4+
5+
Each of them - the car, the person, and the horse - has its own unique way of moving. An interface could be used to define the contract requirement "You need to be able to move," but the interface doesn't define how to move.
6+
7+
### Parts of an interface
8+
9+
In C#, an interface outlines the required properties and methods. An interface doesn't hold any data itself, but it does specify what kind of information should be provided through properties. For instance, in a "Move" interface, 'X' and 'Y' could be properties, representing location coordinates. These properties enable the retrieval or modification of their values.
10+
11+
Additionally, interfaces define methods, which are actions that can be performed. However, they don't provide the implementation for these methods. For example, the "Move" interface could include a contract requiring a "Dance" method. However, what exactly happens when the "Dance" method is called by the objects that use this interface, whether it's a car, person, or horse.
12+
13+
### Use an interface
14+
15+
To utilize an interface, entities like a car, a person, or a horse from the "move" example adopt it as part of their functionality. The car, for instance, needs to understand how to perform all the tasks listed in the interface. This process of adopting an interface by the car is known as 'implementing' the interface. In this process, the car agrees to follow the 'contract' set by the interface, defining its own unique ways to complete the tasks such as moving by "driving." Similarly, the person and the horse would also implement the interface, each defining their own unique ways to 'move' such as "run" or "gallop."
16+
17+
Consider the classes "Person" and "Horse." Both implement the "Move" interface, and it's the responsibility of "Person" and "Horse" classes to define ways to "Move."
18+
19+
In the "Person" class, 'Move' could mean walking, jogging, or running. In the "Horse" class, 'Move' could mean walking, trotting, or galloping. Even though they move in different ways, both classes fulfill the 'Move' job requirement from the "Move" interface.
20+
21+
### Why use an interface?
22+
23+
The properties defined by an interface are useful because they ensure that all classes handle information in the same way. An interface makes your related classes more consistent and easier to work with. As long as they all use the same interface, you can write instructions that work with all kinds of different classes.
24+
25+
Implementing an interface is like signing a contract, agreeing to do certain jobs outlined by the interface. The "contract" enforcement provided by interfaces aids in building complex classes that can be easily maintained and updated.

0 commit comments

Comments
 (0)