• Profile
  • Resume
  • Portfolio
  • Blog
Avatar

Nelson Silva

A human. Being. Coffee lover.

Blog

I Write My Thoughts Here

Interfaces in C#

December 29, 2021 C#

In this post, you will learn how to work with interfaces so that you can use that same knowledge and thus evolve.

  • an interface contains definitions for a group of related features that a class or structure (we’ll talk about structs in the next video) can implement;
  • in short, an interface is like a contract and any class or structure that implements a given interface must implement all its members;
  • it is also important to know that a class or a structure can implement several interfaces.
using System;

namespace Base {
  class Interfaces {
    public void Run() {
      var car = new Car("Nissan", "Juke", "Forward", "Manual", "Diesel", 1461, 175, 110, 25070);
      car.Information();

      /*
        Brand: Nissan
        Model: Juke
        Engine location: Forward
        Transmission: Manual
        Fuel: Diesel
        Displacement: 1461 cc
        Maximum speed: 175 km/h
        Maximum power: 110 cv
        Price: 25070 euros
      */
    }
  }

  class Car : ICar {
    private string brand, model, engineLocation, transmission, fuel;
    private int displacement, maximumSpeed, maximumPower;
    private double price;

    public Car(string brand, string model, string engineLocation, string transmission, string fuel,
               int displacement, int maximumSpeed, int maximumPower, double price) {
      this.brand = brand;
      this.model = model;
      this.engineLocation = engineLocation;
      this.transmission = transmission;
      this.fuel = fuel;
      this.displacement = displacement;
      this.maximumSpeed = maximumSpeed;
      this.maximumPower = maximumPower;
      this.price = price;
    }

    public void Information() {
      Console.WriteLine($"Brand: {brand}");
      Console.WriteLine($"Model: {model}");
      Console.WriteLine($"Engine location: {engineLocation}");
      Console.WriteLine($"Transmission: {transmission}");
      Console.WriteLine($"Fuel: {fuel}");
      Console.WriteLine($"Displacement: {displacement} cc");
      Console.WriteLine($"Maximum speed: {maximumSpeed} km/h");
      Console.WriteLine($"Maximum power: {maximumPower} cv");
      Console.WriteLine($"Price: {price} euros");
    }
  }

  interface ICar {
    void Information();
  }
}

Don’t forget to watch the video and you can always read this post in Portuguese.

Happy coding!

Previous Next

Categories

  • Announcements
  • Python
  • C#
  • Java
  • Go

Latest Posts

  • for Loop in Go
    May 5, 2022
  • Switch in Go
    May 4, 2022
  • Comparison Operators in Go
    May 3, 2022
+351913416022
contact@nelsonsilvadev.com
Porto, Portugal
Nelson Silva © 2022 All Rights Reserved