DotNet-101-CSharpFundamentals
  • Introduction
  • Part 0: Solution Setup
    • 1.0: Solution Setup
  • Part 1: Data Types
    • 1.0: Variables
    • 1.1: Data Types
    • 1.2: Data Types Table
    • 1.3: Challenge Answers
    • 1.4: Strings
    • 1.5: String Challenges
    • 1.6: Challenge Answers
    • 1.7: Integers
    • 1.8: Type Conversion
  • Part 2: Boolean Logic And Conditionals
    • 2.0: Boolean Type
    • 2.1: Boolean Logic
    • 2.2: Boolean Challenges
    • 2.3: Challenge Answers
    • 2.4: if, else if, else
    • 2.5: Switch Statements
    • 2.6: Ternary Operators
  • Part 3: Objects, Methods, C# Fundamentals
    • 3.0 Objects
    • 3.1 Properties
      • Challenge Answers
    • 3.2 Methods
      • Challenge Answers
    • 3.3 Method-Overloading
    • 3.4 Constructors
      • Challenge Answers
    • 3.5 Access Modifiers
  • Part 4: Collections Arrays Control Flow
    • 4.0 Arrays
    • 4.1 Loops
    • 4.2 Dictionaries
    • Challenge Answers
    • 4.3 List
    • 4.3a: Challenge Answers
  • Part 5: Inheritance/More On Object Oriented Programming
    • 5.0 Inheritance
    • 5.1 Polymorphism
    • 5.2 Encapsulation
    • 5.3 Getters And Setters
    • 5.4 Exception Handling
  • Part 6: Structs
    • 6.0 Structs
  • Part 7: Enums
    • 7.0 Enums
  • Part 8: Null Coalescing Operator
    • 8.0 Null Coalescing Operator
  • Part 9: Interfaces
    • 9.0 Interfaces
    • 9.1 Rules
    • 9.2 Code With Intent
  • Part 10: More C#
    • 10.0 DateTime
    • 10.1-LINQ
    • Challenge Answers
  • Part 11: Reference and Value Types
    • 11.0 Reference & Value Types
  • Eleven Fifty Style Guide
Powered by GitBook
On this page
  1. Part 9: Interfaces

9.2 Code With Intent

Previous9.1 RulesNextPart 10: More C#

Last updated 7 years ago

If interfaces don't implement or add any functionality, then why should we use them? Great question. Interface based design gives us several advantages, including easier maintainability, scalability (making it bigger or smaller), and lastly it makes your code more reusable and accessible because of this separation of concerns.

"Separation of Concerns (SoC) is a design principle for separating a computer program into distinct sections, such that each section addresses a separate concern. A concern is "a set of information that affects the code of a computer program." -

Interfaces define a contract or agreement between your application and the other objects. The contract will include what methods and properties are exposed to said objects. Even though the interface will not add any true functionality to these objects, it is benefitting you as a programmer to be planning ahead and coding with intent on exactly how you want your program to work.

For example, say you have a vehicle. All vehicles have similar characteristics but differ enough that we could us an interface that contracts the similarities. Vehicles will have certain differences such as some have 18 wheels, others have 4, others have 2, but they all move, all have an engine, and have doors. These items might be different from one vehicle to another. We can create an interface of vehicle that has these general properties, we can then inherit from the interface to implement them.

Interfaces help us create a layout for what our classes will look like and what they will implement. When dealing with bigger applications and when you use interfaces, it allows us, the programmer, to easily interchange one component for another that is using the same interface.

Let's look at a code example of a vehicle interface:

    namespace InterfaceExample
    {    
        public interface IVehicle    
            {        
                int Doors { get; set; }        
                int Wheels { get; set; }        
                Color VehicleColor { get; set; }        
                int TopSpeed { get; set; }        
                int Cylinders { get; set; }        
                int CurrentSpeed { get; }        
                string DisplayTopSpeed();        
                void Accelerate(int step);    
            }
    }

Properties of interface IVehicle

Methods of interface IVehicle

Doors

DisplayTopSpeed()

Wheels

Accelerate(int step)

Color

Top Speed

Cylinders

Current Speed

Now we can create a motorcycle class based on our IVehicle interface

    public class Motorcycle : IVehicle    
        {       
            private int _currentSpeed = 0;        
            public int Doors { get; set; }        
            public int Wheels { get; set; }        
            public Color VehicleColor { get; set; }        
            public int TopSpeed { get; set; }        
            public int HorsePower { get; set; }        
            public int Cylinders { get; set; }        
            public int CurrentSpeed        
                {            
                    get { return _currentSpeed; }        
                }      
            public Motorcycle(int doors, int wheels, Color color, int topSpeed, int horsePower, int cylinders, int currentSpeed)        
                {            
                    this.Doors = doors;            
                    this.Wheels = wheels;            
                    this.VehicleColor = color;            
                    this.TopSpeed = topSpeed;            
                    this.HorsePower = horsePower;            
                    this.Cylinders = cylinders;            
                    this._currentSpeed = currentSpeed;        
                }        
            public string DisplayTopSpeed()        
                {            
                    return "Top speed is: " + this.TopSpeed;        
                }        
            public void Accelerate(int step)        
                {            
                    this._currentSpeed += step;        
                }    
        }

We could interchange the Motorcycle class with the Truck class or Carr class and have the same functionality of IVehicle all within the same application.

Interface based development can make your life as a developer much easier, and your applications much cleaner, maintainable, and scalable.

Date & Time

Wikipedia
Next: