Skip to content

Commit 539d962

Browse files
committed
2 parents 9f0a3eb + 427185e commit 539d962

File tree

8 files changed

+308
-0
lines changed

8 files changed

+308
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# This workflow is triggered on two events: workflow_dispatch and push.
2+
#
3+
# - The workflow_dispatch event allows you to manually trigger the workflow from GitHub's UI.
4+
# - The push event triggers the workflow whenever there's a push to the master branch, but only
5+
# if the changes include files in the LearnModuleExercises/SampleApps/** directory.
6+
#
7+
# The defaults section sets the default shell for all run commands in the workflow to PowerShell (pwsh).
8+
#
9+
# The workflow consists of a single job named create_zip, which runs on the latest version of Ubuntu.
10+
#
11+
# This job has three steps:
12+
#
13+
# 1. The Checkout step uses the actions/checkout@v4 action to checkout the repository's code onto the
14+
# runner. This is a common first step in most workflows as it allows subsequent steps to operate on
15+
# the codebase.
16+
#
17+
# 2. The Create SampleApps zip step changes the current directory to ./LearnModuleExercises/SampleApps
18+
# and then creates a zip file of all the files in that directory, including those in the .vscode
19+
# subdirectory. The -r option is used to zip directories recursively and the -q option is used to run
20+
# the command quietly without printing a lot of output. The resulting zip file is saved in the
21+
# ../Downloads directory with the name SampleApps.zip.
22+
#
23+
# 3. The Commit and push step uses the Endbug/add-and-commit@v7 action to add the newly created zip file
24+
# to the repository, commit the changes with the message 'Updating Zip for API source files', and then
25+
# push the changes back to the repository. The add input is set to the path of the zip file and the push
26+
# input is set to true to enable pushing.
27+
#
28+
# This workflow is useful for automatically packaging and versioning sample applications whenever changes
29+
# are made to them.
30+
#
31+
name: CreateLP1SamplesZip
32+
on:
33+
workflow_dispatch:
34+
push:
35+
branches:
36+
- 'main'
37+
paths:
38+
- DownloadableCodeProjects/LP1_classes-properties-methods/**
39+
40+
defaults:
41+
run:
42+
shell: pwsh
43+
44+
jobs:
45+
create_zip:
46+
runs-on: ubuntu-latest
47+
steps:
48+
- name: Checkout
49+
uses: actions/checkout@v4
50+
- name: Create LP1 SampleApps zip
51+
run: |
52+
cd ./DownloadableCodeProjects/LP1_classes-properties-methods
53+
rm -f ../Downloads/LP1SampleApps.zip
54+
zip -r -q ../Downloads/LP1SampleApps.zip $(git ls-files)
55+
- name: Commit and push
56+
uses: Endbug/add-and-commit@v7
57+
with:
58+
add: '["DownloadableCodeProjects/Downloads/LP1SampleApps.zip"]'
59+
message: 'Updating Zip with sample app source files'
60+
push: true
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public interface IPerson
2+
{
3+
string Name { get; set; }
4+
int Age { get; set; }
5+
void DisplayInfo();
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace ImplementInterfaces
2+
{
3+
class Program
4+
{
5+
static void Main(string[] args)
6+
{
7+
IPerson student = new Student { Name = "John Doe", Age = 20 };
8+
IPerson teacher = new Teacher { Name = "Jane Smith", Age = 35 };
9+
10+
student.DisplayInfo();
11+
teacher.DisplayInfo();
12+
}
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Student : IPerson
2+
{
3+
public string Name { get; set; } = string.Empty;
4+
public int Age { get; set; } = 0;
5+
6+
public void DisplayInfo()
7+
{
8+
Console.WriteLine($"Student Name: {Name}, Age: {Age}");
9+
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public class Teacher : IPerson
2+
{
3+
public string Name { get; set; } = string.Empty;
4+
public int Age { get; set; } = 0;
5+
6+
public void DisplayInfo()
7+
{
8+
Console.WriteLine($"Teacher Name: {Name}, Age: {Age}");
9+
}
10+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This exercise doesn't require a starter file.
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
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 dont 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

Comments
 (0)