You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/ImplementInterfaces/ImplementInterfaces.csproj
Copy file name to clipboardExpand all lines: Instructions/Labs/l2p2-lp2-m1-exercise-implement-interfaces-project.md
+98-51Lines changed: 98 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,15 +12,19 @@ In this exercise, you will create a console app to define and implement interfac
12
12
13
13
This exercise takes approximately **20** minutes to complete.
14
14
15
+
---
16
+
15
17
## Before you start
16
18
17
19
Before you can start this exercise, you need to:
18
20
19
-
1. Ensure that you have the latest short term support (STS) version of the .NET SDK installed on your computer. You can download the latest versions of the .NET SDK using the following URL: Download .NET
20
-
1. Ensure that you have Visual Studio Code installed on your computer. You can download Visual Studio Code using the following URL: Download Visual Studio Code
21
+
1. Ensure that you have the latest short term support (STS) version of the .NET SDK installed on your computer. You can download the latest versions of the .NET SDK using the following URL: [Download .NET](https://dotnet.microsoft.com/download).
22
+
1. Ensure that you have Visual Studio Code installed on your computer. You can download Visual Studio Code using the following URL: [Download Visual Studio Code](https://code.visualstudio.com/).
21
23
1. Ensure that you have the C# Dev Kit configured in Visual Studio Code.
22
24
23
-
For additional help configuring the Visual Studio Code environment, see Install and configure Visual Studio Code for C# development
25
+
For additional help configuring the Visual Studio Code environment, see [Install and configure Visual Studio Code for C# development](https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code).
26
+
27
+
---
24
28
25
29
## Exercise scenario
26
30
@@ -35,10 +39,18 @@ This exercise includes the following tasks:
35
39
1. Demonstrate interface implementation by creating instances of the classes and calling their methods.
36
40
1. Test the implemented interfaces and their implementations to ensure they work as expected.
37
41
42
+
---
43
+
38
44
## Task 1: Create a new C# project
39
45
40
46
To start, you need to create a new C# project in your development environment. This project will serve as the foundation for implementing interfaces and their respective classes.
41
47
48
+
### What you'll learn
49
+
50
+
- How to create a new C# console application using the .NET CLI.
51
+
52
+
### Steps
53
+
42
54
1. Open Visual Studio Code.
43
55
1. Ensure that the C# Dev Kit extension is installed.
44
56
1. Open the terminal in Visual Studio Code by selecting `View > Terminal`.
@@ -61,86 +73,105 @@ To start, you need to create a new C# project in your development environment. T
61
73
code .
62
74
```
63
75
64
-
### Check your work: Create a new C# project
76
+
### Check your work
65
77
66
78
Ensure that the project has been created successfully by verifying the presence of the `Program.cs` file in the project directory. You should also see the project structure in the Visual Studio Code Explorer pane.
67
79
80
+
---
81
+
68
82
## Task 2: Define an interface with method signatures and properties
69
83
70
-
Next, you will define an interface that includes method signatures and properties. This interface will be used to enforce a contract for any class that implements it.
84
+
Next, you will define an interface that includes method signatures and properties. This interface will be used to enforce a contract for any class that implements it. The code defines an interface in C# which shows interfaces enforcing consistent behavior across classes.
85
+
86
+
### Steps
71
87
72
88
1. In the `ImplementInterfaces` project, create a new file named `IPerson.cs`.
73
89
1. Add the following code to define the `IPerson` interface:
74
90
75
91
```csharp
76
-
publicinterfaceIPerson
92
+
namespaceImplementInterfaces
77
93
{
78
-
stringName { get; set; }
79
-
intAge { get; set; }
80
-
voidDisplayInfo();
94
+
publicinterfaceIPerson
95
+
{
96
+
stringName { get; set; }
97
+
intAge { get; set; }
98
+
voidDisplayInfo();
99
+
}
81
100
}
82
101
```
83
102
84
-
### Check your work: Define an interface
103
+
### Check your work
85
104
86
105
Verify that the `IPerson` interface is correctly defined by checking the `IPerson.cs` file. The interface should include the `Name` and `Age` properties, as well as the `DisplayInfo` method signature.
87
106
107
+
---
108
+
88
109
## Task 3: Implement the defined interface in a class
89
110
90
-
Now, you will create a class that implements the `IPerson` interface. This class will provide concrete implementations for the interface members.
111
+
Now, you will create a class that implements the `IPerson` interface. This class will provide concrete implementations for the interface members. The code in this step implements an interface in a class and provides concrete implementations for the interface.
91
112
92
-
1. In the `ImplementInterfaces` project, create a new file named `Student.cs`.
93
-
1. Add the following code to implement the `IPerson` interface in the `Student` class. Note that we initialize the properties with default values to avoid nullable warnings:
Ensure that the `Student` class correctly implements the `IPerson` interface by checking the `Student.cs` file. The class should provide implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method.
114
136
137
+
---
138
+
115
139
## Task 4: Create another class that implements different behavior
You will now create another class that implements the `IPerson` interface but with different behavior. Task 4 implements the same interface in multiple classes. This demonstrates how to provide unique behavior for each class using an interface.
142
+
143
+
### Steps
118
144
119
145
1. In the `ImplementInterfaces` project, create a new file named `Teacher.cs`.
120
146
1. Add the following code to implement the `IPerson` interface in the `Teacher` class:
In the teacher class the `DisplayInfo` method differs from the version in the student class with teh console output starting with "Teacher Name."
136
-
137
-
### Check your work: Create another class that implements the same interface
164
+
### Check your work
138
165
139
166
Verify that the `Teacher` class correctly implements the `IPerson` interface by checking the `Teacher.cs` file. The class should provide implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method.
140
167
168
+
---
169
+
141
170
## Task 5: Demonstrate interface implementation
142
171
143
-
In this task, you will demonstrate the use of the interface by creating instances of the `Student` and `Teacher` classes and calling their methods.
172
+
In this task, you will demonstrate the use of the interface by creating instances of the `Student` and `Teacher` classes and calling their methods. This demonstrates how to use polymorphism to treat objects of different classes as the same interface type.
173
+
174
+
### Steps
144
175
145
176
1. Open the `Program.cs` file in the `ImplementInterfaces` project.
146
177
1. Replace the existing code with the following:
@@ -154,8 +185,8 @@ In this task, you will demonstrate the use of the interface by creating instance
@@ -164,33 +195,49 @@ In this task, you will demonstrate the use of the interface by creating instance
164
195
}
165
196
```
166
197
167
-
### Check your work: Demonstrate interface implementation
198
+
### Check your work
168
199
169
-
You should see the `dotnet run` output displaying the information for both the student and the teacher, demonstrating the interface implementation.
200
+
Run the application using the following command:
201
+
202
+
```bash
203
+
dotnet run
204
+
```
205
+
206
+
You should see the output displaying the information for both the student and the teacher, demonstrating the interface implementation.
207
+
208
+
---
170
209
171
210
## Task 6: Test the implemented interfaces
172
211
173
-
Finally, you will test the implemented interfaces and their respective classes to ensure they function correctly.
212
+
Next, you will test the implemented interfaces and their respective classes to ensure they function correctly. This section of code demonstrates how to test interface implementations in a C# application.
213
+
214
+
### Steps
174
215
175
216
1. Ensure that the `Program.cs` file contains the code to create instances of `Student` and `Teacher` and calls their `DisplayInfo` methods.
176
217
1. Run the application again using the following command:
177
218
178
-
```bash
179
-
dotnet run
180
-
```
219
+
```bash
220
+
dotnet run
221
+
```
181
222
182
223
1. Verify the output to ensure that the information for both the student and the teacher is displayed correctly.
183
224
184
-
### Check your work: Test the implemented interfaces
225
+
### Check your work
185
226
186
227
Confirm that the application runs without errors and displays the correct information for both the student and the teacher. The output should look similar to the following:
187
228
188
229
```console
189
-
Student Name: John Doe, Age: 20
190
-
Teacher Name: Jane Smith, Age: 35
230
+
Student Name: Eric Solomon, Age: 20
231
+
Teacher Name: Kayla Lewis, Age: 35
191
232
```
192
233
193
-
In this exercise, you learned how to define and implement interfaces in C# to enforce consistent behavior across multiple classes. By creating a shared interface and implementing it in different classes, you ensured that each class adhered to a common contract while allowing forunique implementations. You also demonstrated the use of interfaces by creating instances of these classes and testing their functionality. This approach is a powerful way to design flexible and maintainable applicationsin object-oriented programming.
234
+
---
235
+
236
+
## Key learnings
237
+
238
+
This exercise demonstrated how to use interfaces in C# to enforce consistent behavior across multiple classes. By implementing the `IPerson` interface in both `Student` and `Teacher` classes, you explored how interfaces enable polymorphism and allow different classes to share a common contract while maintaining unique behavior. This approach is essential for building scalable and maintainable applications.
0 commit comments