Learning Path Details
Foundation in C#: Object Oriented Programming Part 2 from Tim Corey
Description:
Module 6 of the Complete Foundation in C# Course Series, we continue learning about object-oriented programming. We cover interfaces and the concept of inheritance, access modifiers, the concept of abstract classes, and method overrides, overloaded methods and extension methods, generics, and events.
Key Takeaways:
We learn the remaining key concepts of object-oriented programming, including interfaces, inheritance, modifiers, abstract, overrides, overloads, extensions, generics, and events.
We also complete a mini-project on inheritance; a card game project to demonstrate skills in access modifiers, abstract classes, and method overrides; a mini-project on extension methods; and a final mini-project putting it all together.
Technologies Learned:
- .NET;
- .NET Framework;
- .NET Framework 4.7.2;
- Abstract Classes;
- Access Modifiers;
- C#;
- Event Handlers;
- Event Listeners;
- Events;
- Extension Methods;
- Generics;
- Inheritance;
- Interfaces;
- Method Overrides;
- Overloaded Methods;
This learning resource was completed on 9/13/2021.
Find the Resource here:
Practice Projects
Mini Project
Description:
The homework from the Lesson 14, Mini Project, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to recreate the project we jsut did without looking back a the video. Make sure to make small tweaks.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
https://github.com/Spartan-CSharp/OOP2-GenericsEventsMiniProject
Code Snippet:
using System;
using System.Collections.Generic;
namespace WrapUp
{
internal class Program
{
private static void Main()
{
DataAccess.BadEntryFound += DataAccess_BadEntryFound;
List<PersonModel> people = new List<PersonModel> {
new PersonModel { FirstName = "Tim", LastName = "Coreydarnit", Email = "tim@iamtimcorey.com"},
new PersonModel { FirstName = "Sue", LastName = "Storm", Email = "sue@stormy.com"},
new PersonModel { FirstName = "John", LastName = "Smith", Email = "John4537@aol.com"}
};
List<CarModel> cars = new List<CarModel> {
new CarModel { Manufacturer = "Toyota", Model = "DARNCorolla" },
new CarModel { Manufacturer = "Toyota", Model = "Highlander" },
new CarModel { Manufacturer = "Ford", Model = "heckMustang" }
};
people.SaveToCSV(@"D:\Pierre\source\repos\Complete Foundation in C# Course Series\6 Object Oriented Programming Part 2\Homework\WrapUpHomeworkApp\people.csv");
cars.SaveToCSV(@"D:\Pierre\source\repos\Complete Foundation in C# Course Series\6 Object Oriented Programming Part 2\Homework\WrapUpHomeworkApp\cars.csv");
_ = Console.ReadLine();
}
private static void DataAccess_BadEntryFound(object sender, string e)
{
if ( e == "WrapUpHomework.PersonModel" )
{
PersonModel person = (PersonModel)sender;
Console.WriteLine($"Bad Word(s) found in {e} {person.FirstName} {person.LastName}!");
}
else if ( e == "WrapUpHomework.CarModel" )
{
CarModel car = (CarModel)sender;
Console.WriteLine($"Bad Word(s) found in {e} {car.Manufacturer} {car.Model}!");
}
else
{
Console.WriteLine($"Bad Word(s) found in {e}!");
}
Console.WriteLine();
}
}
}
Events
Description:
The homework from the Lesson 13, Events, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create an event in a class and then consume that class. Attach a listener to the event and have it write to the console.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
using System;
namespace Events
{
internal class Program
{
private static void Main()
{
PersonModel person = new PersonModel("Tim", "Corey");
person.LastNameChanged += Person_LastNameChanged;
Console.WriteLine(person.FullName);
person.LastName = "Storm";
Console.WriteLine(person.FullName);
_ = Console.ReadLine();
}
private static void Person_LastNameChanged(object sender, string e)
{
PersonModel model = (PersonModel)sender;
Console.WriteLine();
Console.WriteLine($"{model.FullName}'s last name has changed: {e}");
Console.WriteLine();
}
}
public class PersonModel
{
public event EventHandler<string> LastNameChanged;
public string FirstName { get; set; }
private string _lastName;
public string LastName
{
get
{
return _lastName;
}
set
{
LastNameChanged?.Invoke(this, $"Last Name has been changed from {_lastName} to {value}.");
_lastName = value;
}
}
public string FullName
{
get
{
return $"{FirstName} {LastName}";
}
}
public PersonModel(string firstName, string lastName)
{
FirstName = firstName;
_lastName = lastName;
}
}
}
Generics
Description:
The homework from the Lesson 12, Generics, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create a generic method that takes in an item and calls the ToString() method and prints that value to the Console.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
using System;
namespace Generics
{
internal class Program
{
private static void Main()
{
string teststring = "A String";
int testint = 42;
bool testbool = false;
double testdouble = 98.6;
PersonModel testpersonmodel = new PersonModel { FirstName = "Tim", LastName = "Corey" };
string stringstringified = Stringify(teststring);
string intstringified = Stringify(testint);
string boolstringified = Stringify(testbool);
string doublestringified = Stringify(testdouble);
string personmodelstringified = Stringify(testpersonmodel);
Console.WriteLine($"{teststring} stringified is {stringstringified}");
Console.WriteLine($"{testint} stringified is {intstringified}");
Console.WriteLine($"{testbool} stringified is {boolstringified}");
Console.WriteLine($"{testdouble} stringified is {doublestringified}");
Console.WriteLine($"{testpersonmodel} stringified is {personmodelstringified}");
_ = Console.ReadLine();
}
private static string Stringify<T>(T item)
{
string output = item.ToString();
return output;
}
}
}
Mini-Project
Description:
The homework from the Lesson 11, Mini Project, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to recreate the project we did for this lesson but without looking back over what Tim did. Tweak it slightly as well.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
https://github.com/Spartan-CSharp/OOP2-ExtensionsOverloadsMiniProject
Code Snippet:
using System;
namespace ExtensionsOverloads
{
internal class Program
{
private static void Main()
{
PersonModel person = new PersonModel
{
FirstName = "What is your first name: ".RequestString(),
LastName = "What is your last name: ".RequestString(),
Age = "What is your age: ".RequestInt(1, 120),
Salary = "What is your salary: ".RequestDecimal()
};
_ = person.PrintInfo();
_ = Console.ReadLine();
}
}
}
Extension Methods
Description:
The homework from the Lesson 10, Extension Methods, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create the following chain using extension methods: person.SetDefaultAge().PrintInfo();
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
using System;
namespace ExtensionMethods
{
public static class PersonExtensions
{
public static PersonModel SetDefaultAge(this PersonModel person)
{
person.Age = 25;
return person;
}
public static PersonModel PrintInfo(this PersonModel person)
{
Console.WriteLine($"{person.FullName} is {person.Age} years old.");
return person;
}
}
}
Method Overloading
Description:
The homework from the Lesson 9, Method Overloading, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create an EmployeeModel class. Create three different constructors that take in different amounts of data.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
namespace MethodOverload
{
public class EmployeeModel
{
public string FirstName
{
get; set;
}
public string LastName
{
get; set;
}
public string Email
{
get; set;
}
public decimal Salary
{
get; set;
}
public string FullName
{
get
{
return $"{FirstName} {LastName}";
}
}
public EmployeeModel()
{
}
public EmployeeModel(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public EmployeeModel(string firstName, string lastName, string email)
{
FirstName = firstName;
LastName = lastName;
Email = email;
}
public EmployeeModel(string firstName, string lastName, decimal salary)
{
FirstName = firstName;
LastName = lastName;
Salary = salary;
}
}
}
Mini Project
Description:
The homework from the Lesson 8, Mini-Project, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to build a similar project to what we did here but change it just a bit so you are sure you understand it.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Class Library;
- Console Application;
Find the Project here:
https://github.com/Spartan-CSharp/OOP2-ModifierAbstractOverrideMiniProject
Code Snippet:
using System;
using System.Collections.Generic;
using System.Linq;
using CardGameLibrary.Enums;
namespace CardGameLibrary.Models
{
public abstract class DeckModel
{
internal List<PlayingCardModel> _fullDeck = new List<PlayingCardModel>();
internal List<PlayingCardModel> _drawPile = new List<PlayingCardModel>();
internal List<PlayingCardModel> _discardPile = new List<PlayingCardModel>();
public void ShuffleDeck()
{
Random rnd = new Random();
_drawPile = _fullDeck.OrderBy(x => rnd.Next()).ToList();
}
public abstract List<PlayingCardModel> DealCards();
protected PlayingCardModel DrawOneCard()
{
PlayingCardModel output = _drawPile.First();
_ = _drawPile.Remove(output);
return output;
}
private protected void CreateDeck()
{
_fullDeck.Clear();
for ( int suit = 0; suit < 4; suit++ )
{
for ( int value = 0; value < 13; value++ )
{
PlayingCardModel card = new PlayingCardModel((CARDSUIT)suit, (CARDVALUE)value);
_fullDeck.Add(card);
}
}
}
}
}
Method Overriding
Description:
The homework from the Lesson 7, Method Overriding, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create a Person class and override the ToString method in it. Make it return the user's first and last name.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
namespace MethodOverride
{
public class PersonModel
{
public string FirstName
{
get; set;
}
public string LastName
{
get; set;
}
public override string ToString()
{
return $"{FirstName} {LastName}";
}
}
}
Abstract Classes
Description:
The homework from the Lesson 6, Abstract Classes, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create an Abstract Class that has an Interface applied to it. When instantiating the child class, store it using the interface type.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
using System;
namespace AbstractClass
{
public abstract class Vehicle : IInventoryItem
{
public decimal SellPrice
{
get; set;
}
public decimal PurchasePrice
{
get; set;
}
public int QuantityOnHand
{
get; set;
}
public string Manufacturer
{
get; set;
}
public string Model
{
get; set;
}
public string VIN
{
get; set;
}
public decimal CalculateGrossProfit()
{
throw new NotImplementedException();
}
public void SellItem()
{
throw new NotImplementedException();
}
}
}
Access Modifiers
Description:
The homework from the Lesson 5, Access Modifiers, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create a Class Library and a Console Application. Create a public class with members that have different modifiers. Try each out.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Class Library;
- Console Application;
Find the Project here:
Code Snippet:
using System;
using AccessModifiersLibrary;
namespace AccessModifiers
{
internal class Program
{
private static void Main()
{
Person person = new Person();
Manager manager = new Manager();
CEO ceo = new CEO();
// Class Pet not accessible as it is internal
Pet cat = new Pet();
// Class Dog not accessible as it is internal
Dog scruffy = new Dog();
// person.FirstName is accessible as it is public
Console.WriteLine($"{person.FirstName}");
// person.LastName is not accessible as it is protected
Console.WriteLine($"{person.LastName}");
// person.Age is not accessible as it is internal
Console.WriteLine($"{person.Age}");
// person.NetWorth is not accessible as it is protected internal
Console.WriteLine($"{person.NetWorth}");
// person.SSN is not accessible as it is private
Console.WriteLine($"{person.SSN}");
// person.Height is not accessible as it is private protected
Console.WriteLine($"{person.Height}");
// manager.FirstName is accessible as it is public
Console.WriteLine($"{manager.FirstName}");
// manager.LastName is not accessible as it is protected
Console.WriteLine($"{manager.LastName}");
// manager.Age is not accessible as it is internal
Console.WriteLine($"{manager.Age}");
// manager.NetWorth is not accessible as it is protected internal
Console.WriteLine($"{manager.NetWorth}");
// manager.SSN is not accessible as it is private
Console.WriteLine($"{manager.SSN}");
// manager.Height is not accessible as it is private protected
Console.WriteLine($"{manager.Height}");
// manager.PrintProperties is accessible as it is public
manager.PrintProperties();
// ceo.FirstName is accessible as it is public
Console.WriteLine($"{ceo.FirstName}");
// ceo.LastName is not accessible as it is protected
Console.WriteLine($"{ceo.LastName}");
// ceo.Age is not accessible as it is internal
Console.WriteLine($"{ceo.Age}");
// ceo.NetWorth is not accessible as it is protected internal
Console.WriteLine($"{ceo.NetWorth}");
// ceo.SSN is not accessible as it is private
Console.WriteLine($"{ceo.SSN}");
// ceo.Height is not accessible as it is private protected
Console.WriteLine($"{ceo.Height}");
// ceo.PrintProperties is accessible as it is public
ceo.PrintProperties();
_ = Console.ReadLine();
}
}
}
Mini-Project
Description:
The homework from the Lesson 4, Mini-Project, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to build a similar project to what we did here but change it just a bit so we are sure we understand it.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
https://github.com/Spartan-CSharp/OOP2-InheritanceInterfaceMiniProject
Code Snippet:
using System;
using System.Collections.Generic;
using InheritanceMiniProject.Interfaces;
using InheritanceMiniProject.Models;
namespace InheritanceMiniProject
{
internal class Program
{
private static void Main()
{
List<IRentable> rentables = new List<IRentable>();
List<IPurchaseable> purchaseables = new List<IPurchaseable>();
BeachBallModel beachball = new BeachBallModel { BallSize = 15, ItemName = "Wilson Beach Ball", PurchasePrice = 25.00M, QuantityInStock = 10 };
BookModel book = new BookModel { Author = "Charles Dickens", ItemName = "A Tale of Two Cities", NumberOfPages = 658, PurchasePrice = 7.99M, QuantityInStock = 3 };
CarModel car = new CarModel { ItemName = "Chevy Malibu", NumberOfSeats = 4, PurchasePrice = 25000.00M, QuantityInStock = 7, RentalPrice = 125.00M, VehicleIdentificationNumber = "2C56DFG66734A" };
ExcavatorModel excavator = new ExcavatorModel { ItemName = "Backhoe", QuantityInStock = 2, RentalPrice = 250.00M, VehicleIdentificationNumber = "14H34CAB23HE5" };
_ = new InventoryItemModel { ItemName = "Widget", QuantityInStock = 25000 };
MotorcycleModel motorcycle = new MotorcycleModel { ItemName = "Voodoo", PurchasePrice = 17000.00M, QuantityInStock = 1, RentalPrice = 99.75M, RideType = "Low Rider", VehicleIdentificationNumber = "2453DCGB7G45F" };
SunscreenModel sunscreen = new SunscreenModel { BottleSize = 3.4, ItemName = "Neutrogena Sunscreen", PurchasePrice = 9.99M, QuantityInStock = 200 };
TowelModel towel = new TowelModel { ItemName = "Beach Towel", PurchasePrice = 35.99M, QuantityInStock = 75, RentalPrice = 2.00M, TowelLength = 72, TowelWidth = 30 };
TruckModel truck = new TruckModel { BedLength = 6, ItemName = "Ram 1500", PurchasePrice = 35000.00M, QuantityInStock = 5, RentalPrice = 175.00M, VehicleIdentificationNumber = "ADF1234DK9E8D" };
VehicleModel vehicle = new VehicleModel { ItemName = "Moped", PurchasePrice = 2500.00M, QuantityInStock = 5, RentalPrice = 50.00M, VehicleIdentificationNumber = "1234ADBD5643D" };
rentables.Add(car);
rentables.Add(excavator);
rentables.Add(motorcycle);
rentables.Add(towel);
rentables.Add(truck);
rentables.Add(vehicle);
purchaseables.Add(beachball);
purchaseables.Add(book);
purchaseables.Add(car);
purchaseables.Add(motorcycle);
purchaseables.Add(sunscreen);
purchaseables.Add(towel);
purchaseables.Add(truck);
purchaseables.Add(vehicle);
Console.WriteLine("General Store");
Console.WriteLine();
string moretransactions;
do
{
Console.Write("Do you want to rent, buy, or return (rent/buy/return): ");
string rentorbuy = Console.ReadLine();
if ( rentorbuy.ToLower() == "rent" )
{
foreach ( IRentable rentable in rentables )
{
Console.Write($"Did you want to rent a {rentable.ItemName} for ${rentable.RentalPrice} (yes/no): ");
string wanttorent = Console.ReadLine();
if ( wanttorent.ToLower() == "yes" )
{
rentable.Rent();
Console.WriteLine();
}
}
Console.WriteLine();
}
else if ( rentorbuy.ToLower() == "buy" )
{
foreach ( IPurchaseable purchaseable in purchaseables )
{
Console.Write($"Did you want to purchase a {purchaseable.ItemName} for ${purchaseable.PurchasePrice} (yes/no): ");
string wanttobuy = Console.ReadLine();
if ( wanttobuy.ToLower() == "yes" )
{
purchaseable.Puchase();
Console.WriteLine();
}
}
Console.WriteLine();
}
else if ( rentorbuy.ToLower() == "return" )
{
foreach ( IRentable rentable in rentables )
{
Console.Write($"Did you want to return a {rentable.ItemName} (yes/no): ");
string wanttoreturn = Console.ReadLine();
if ( wanttoreturn.ToLower() == "yes" )
{
rentable.Return();
Console.WriteLine();
}
}
Console.WriteLine();
}
else
{
Console.WriteLine("I did not understand.");
Console.WriteLine();
}
Console.Write("Did you want to do anything else (yes/no): ");
moretransactions = Console.ReadLine();
} while ( moretransactions.ToLower() == "yes" );
_ = Console.ReadLine();
}
}
}
Interfaces
Description:
The homework from the Lesson 3, Interfaces, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create an IRun Interface and apply it to a Person Class and an Animal Class. Store both types in a List
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
namespace ConsoleUI
{
public interface IRun
{
string StartRunning();
string StopRunning();
}
}
Inheritance
Description:
The homework from the Lesson 2, Inheritance, of Module 6, Object-Oriented Programming, Part 2, of the Complete Foundation in C# Course Series from Tim Corey. We are to create a Vehicle Class, a Car Class, a Boat Class, and a Motorcycle Class. Identify what inheritance should happen, if any, and wire it up.
Technologies Used:
- .NET;
- .NET Framework;
- .NET Framework 4.8;
- C#;
- Console Application;
Find the Project here:
Code Snippet:
namespace ConsoleUI
{
internal class Program
{
private static void Main()
{
}
}
internal class Vehicle
{
internal int NumberOfSeats
{
get; set;
}
internal string Engine
{
get; set;
}
internal void StartEngine()
{
}
internal void StopEngine()
{
}
}
internal class Car : Vehicle
{
internal int NumberOfDoors
{
get; set;
}
internal int NumberOfWheels
{
get; set;
}
}
internal class Motorcycle : Vehicle
{
}
internal class Boat : Vehicle
{
}
}