|
| 1 | +--- |
| 2 | +lab: |
| 3 | + title: 'Exercise - Implement Interfaces in a Project' |
| 4 | + description: 'Define and implement interfaces in C# to enforce consistent behavior across multiple classes, including explicit interface implementations.' |
| 5 | +--- |
| 6 | + |
| 7 | +# Implement Interfaces in a Project |
| 8 | + |
| 9 | +In object-oriented programming, interfaces define a contract that classes can implement. They specify method signatures and properties that implementing classes must provide. This allows for consistent behavior across different types while enabling flexibility in implementation. In C#, interfaces are defined using the `interface` keyword, and classes implement them using the `: InterfaceName` syntax. |
| 10 | + |
| 11 | +In this exercise, you will create a console app to define and implement interfaces, including explicit interface implementations, to ensure consistent behavior across various components of an application. |
| 12 | + |
| 13 | +This exercise takes approximately **20** minutes to complete. |
| 14 | + |
| 15 | +## Before you start |
| 16 | + |
| 17 | +Before you can start this exercise, you need to: |
| 18 | + |
| 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 C# Dev Kit configured in Visual Studio Code. |
| 22 | + |
| 23 | +For additional help configuring the Visual Studio Code environment, see Install and configure Visual Studio Code for C# development |
| 24 | + |
| 25 | +## Exercise scenario |
| 26 | + |
| 27 | +Suppose you're a software developer at a tech company working on a new project. Your team needs to define common behaviors across different classes using interfaces. To ensure consistent behavior, you decide to create and implement interfaces, including explicit interface implementations, in a simple console application. |
| 28 | + |
| 29 | +This exercise includes the following tasks: |
| 30 | + |
| 31 | +1. Create a new C# project. |
| 32 | +1. Define an interface with method signatures and properties. |
| 33 | +1. Implement the defined interface in a class. |
| 34 | +1. Create another class that implements the same interface with different behavior. |
| 35 | +1. Demonstrate interface implementation by creating instances of the classes and calling their methods. |
| 36 | +1. Test the implemented interfaces and their implementations to ensure they work as expected. |
| 37 | + |
| 38 | +## Task 1: Create a new C# project |
| 39 | + |
| 40 | +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 | + |
| 42 | +1. Open Visual Studio Code. |
| 43 | +1. Ensure that the C# Dev Kit extension is installed. |
| 44 | +1. Open the terminal in Visual Studio Code by selecting `View > Terminal`. |
| 45 | +1. Navigate to the directory where you want to create your project. |
| 46 | +1. Run the following command to create a new console application: |
| 47 | + |
| 48 | + ```bash |
| 49 | + dotnet new console -n ImplementInterfaces |
| 50 | + ``` |
| 51 | + |
| 52 | +1. Navigate into the newly created project directory: |
| 53 | + |
| 54 | + ```bash |
| 55 | + cd ImplementInterfaces |
| 56 | + ``` |
| 57 | + |
| 58 | +1. Open the project in Visual Studio Code: |
| 59 | + |
| 60 | + ```bash |
| 61 | + code . |
| 62 | + ``` |
| 63 | + |
| 64 | +### Check your work: Create a new C# project |
| 65 | + |
| 66 | +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 | + |
| 68 | +## Task 2: Define an interface with method signatures and properties |
| 69 | + |
| 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. |
| 71 | + |
| 72 | +1. In the `ImplementInterfaces` project, create a new file named `IPerson.cs`. |
| 73 | +1. Add the following code to define the `IPerson` interface: |
| 74 | + |
| 75 | + ```csharp |
| 76 | + public interface IPerson |
| 77 | + { |
| 78 | + string Name { get; set; } |
| 79 | + int Age { get; set; } |
| 80 | + void DisplayInfo(); |
| 81 | + } |
| 82 | + ``` |
| 83 | + |
| 84 | +### Check your work: Define an interface |
| 85 | + |
| 86 | +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 | + |
| 88 | +## Task 3: Implement the defined interface in a class |
| 89 | + |
| 90 | +Now, you will create a class that implements the `IPerson` interface. This class will provide concrete implementations for the interface members. |
| 91 | + |
| 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: |
| 94 | + |
| 95 | + ```csharp |
| 96 | + public class Student : IPerson |
| 97 | + { |
| 98 | + public string Name { get; set; } = string.Empty; |
| 99 | + public int Age { get; set; } = 0; |
| 100 | + |
| 101 | + public void DisplayInfo() |
| 102 | + { |
| 103 | + Console.WriteLine($"Student Name: {Name}, Age: {Age}"); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | +> **Note:** |
| 109 | +> In C#, the code `public string Name { get; set; } = string.Empty;` and `public int Age { get; set; } = 0;` sets default values for the properties (`Name` starts as an empty string, and `Age` starts as 0). This helps avoid warnings from the compiler about "nullable" issues. If you don’t set these default values (e.g., just use `public string Name { get; set; }`), your code will still work, but the compiler will warn you that these properties might not be initialized before being used. |
| 110 | + |
| 111 | +### Check your work: Implement the defined interface |
| 112 | + |
| 113 | +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 | + |
| 115 | +## Task 4: Create another class that implements different behavior |
| 116 | + |
| 117 | +You will now create another class that implements the `IPerson` interface but with different behavior. |
| 118 | + |
| 119 | +1. In the `ImplementInterfaces` project, create a new file named `Teacher.cs`. |
| 120 | +1. Add the following code to implement the `IPerson` interface in the `Teacher` class: |
| 121 | + |
| 122 | + ```csharp |
| 123 | + public class Teacher : IPerson |
| 124 | + { |
| 125 | + public string Name { get; set; } = string.Empty; |
| 126 | + public int Age { get; set; } = 0; |
| 127 | + |
| 128 | + public void DisplayInfo() |
| 129 | + { |
| 130 | + Console.WriteLine($"Teacher Name: {Name}, Age: {Age}"); |
| 131 | + } |
| 132 | + } |
| 133 | + ``` |
| 134 | + |
| 135 | +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 |
| 138 | + |
| 139 | +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 | + |
| 141 | +## Task 5: Demonstrate interface implementation |
| 142 | + |
| 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. |
| 144 | + |
| 145 | +1. Open the `Program.cs` file in the `ImplementInterfaces` project. |
| 146 | +1. Replace the existing code with the following: |
| 147 | + |
| 148 | + ```csharp |
| 149 | + using System; |
| 150 | + |
| 151 | + namespace ImplementInterfaces |
| 152 | + { |
| 153 | + class Program |
| 154 | + { |
| 155 | + static void Main(string[] args) |
| 156 | + { |
| 157 | + IPerson student = new Student { Name = "John Doe", Age = 20 }; |
| 158 | + IPerson teacher = new Teacher { Name = "Jane Smith", Age = 35 }; |
| 159 | + |
| 160 | + student.DisplayInfo(); |
| 161 | + teacher.DisplayInfo(); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + ``` |
| 166 | + |
| 167 | +### Check your work: Demonstrate interface implementation |
| 168 | + |
| 169 | +You should see the `dotnet run` output displaying the information for both the student and the teacher, demonstrating the interface implementation. |
| 170 | + |
| 171 | +## Task 6: Test the implemented interfaces |
| 172 | + |
| 173 | +Finally, you will test the implemented interfaces and their respective classes to ensure they function correctly. |
| 174 | + |
| 175 | +1. Ensure that the `Program.cs` file contains the code to create instances of `Student` and `Teacher` and calls their `DisplayInfo` methods. |
| 176 | +1. Run the application again using the following command: |
| 177 | + |
| 178 | + ```bash |
| 179 | + dotnet run |
| 180 | + ``` |
| 181 | + |
| 182 | +1. Verify the output to ensure that the information for both the student and the teacher is displayed correctly. |
| 183 | + |
| 184 | +### Check your work: Test the implemented interfaces |
| 185 | + |
| 186 | +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 | + |
| 188 | +```console |
| 189 | +Student Name: John Doe, Age: 20 |
| 190 | +Teacher Name: Jane Smith, Age: 35 |
| 191 | +``` |
| 192 | + |
| 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 for unique 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 applications in object-oriented programming. |
| 194 | + |
| 195 | +## Clean up |
| 196 | + |
| 197 | +Now that you've finished the exercise, consider archiving your project files for review at a later time. Having your own projects available for review can be a valuable resource when you're learning to code. Also, building up a portfolio of projects can be a great way to demonstrate your skills to potential employers. |
0 commit comments