Shining Star Services LLC
C# Custom LinkedList Console Application and Abstract Base Class and Method - Learning C# - Part 1
One of the best ways to learn a new technology is by using it. In this tutorial, you will create a base class or abstract class, a derived or inherited class. You will work with self-referencing members, a constructor, if statements, for loops, null values, conditional operators, mathematical operators, relational operators, equality operators, and assignment operators. These concepts are quite basic to programming, but if you're learning, they're new to you.

In our example we will create an abstract Inventory class. The Inventory class will have two functions: Category and Item. We will create a derived class, LinkedList, that uses the Inventory class as a linked list. This class will use custom methods, not built-in methods. In the LinkedList class we will create methods to add inventory items to the linked list, as well as remove them from the top of the stack or the bottom of the stack. We will also create methods to print in forward or reverse orders. And finally, we will create a method to allow searching the list by category or item and display the search results.

First, let's create a console application. In Visual Studio, select File, New, and Project. Select the Visual C# category and the Console Application option.

Name your project CSLinkedList and the solution name CSLinkedList. If you select the checkbox to "Create directory for solution," a new directory named CSLinkedList will be created, otherwise, your solution will be put inside the Location you designate.



using System;

namespace CSLinkedList
{

At the top of our project, you will see we need one .NET component, System. That is referenced by default when your project is created. If you open up the References list in the project explorer, you will see the list of libraries that are included by default. These add .NET framework functionality to your project.



namespace has been created by default. Namespaces help organize your code and provide a way to create globally unique types.
   public abstract class Inventory
   {

   public abstract string Category();

   public string inventoryItem = string.Empty;
   public string Item()
   {
   return inventoryItem;
   }
   }

One of the key concepts of object oriented programming is Inheritance along with Abstraction. Inheritance allows you to create a class that allows you to reuse behaviors and definitions.

We first want to create a base class, Inventory. That means that it is a parent class. To make the Inventory class a base class, we use the keyword "abstract." This means any abstract methods defined in the base class must be implemented in the inherited or derived class. If you do not wish to require implementation, use the "virtual" keyword which means that the derived class "can" override the method with its own implementation, but does not have to.

In our above example, we have an abstract method "Category" and an implemented string and method: inventoryItem and Item(). Notice we do not define an implementation for the Category() method. The implementation must be defined by the derived class.
 
Our linked list will use the Item() and Category() for retrieving our Inventory item names.

In Part 2 of our series, we will setup our LinkedList derived class and define our properties and setup our constructor.

© Copyright 1997-2017 Shining Star Services LLC, Nannette Thacker. All Rights Reserved.