loading...
Cover image for C# Interface Basics

C# Interface Basics

kironroy profile image Kiron Roy Updated on ・1 min read
  • Interfaces are useful when you want to store in a <List> similar things that cannot quite inherit from each other
  • Interfaces are like a contract. Everything inside the interface has to be implemented.
  • Classes can implement many interfaces separated by a comma.

Below is some sample code. Note that all the classes are in one file for simplicity. Classes should be moved to separate files.

using System;
using System.Collections.Generic;

namespace InterfaceHW
{
    class Program
    {
        static void Main(string[] args)
        {
            List<IRun> livingbeings = new List<IRun>();

            Person ritaHuman = new Person();
            Person sitaHuman = new Person();
            Person mitaHuman = new Person();

            Animal clydeMaineCoonCat = new Animal();
            Animal maxGoldenRetriever = new Animal();
            Animal apolloGazelle = new Animal();

            livingbeings.Add(ritaHuman);
            livingbeings.Add(sitaHuman);
            livingbeings.Add(mitaHuman);

            livingbeings.Add(clydeMaineCoonCat);
            livingbeings.Add(maxGoldenRetriever);
            livingbeings.Add(apolloGazelle);

            foreach (IRun livingbeing in livingbeings)
            {
                //livingbeing. only uses what the IRun interface has in its contract
                livingbeing.Run();

            }

            Console.ReadLine();
        }
    }

    public class Person : IRun
    {
        public string SpeaksThisLanguage { get; set; }
        public int BiPedal { get; set; }

        public void Run()
        {
            Console.WriteLine("Humans run ");
        }
    }

    public class Animal : IRun
    {
        public void Run()
        {
            Console.WriteLine("Animals run");
        }

        enum Diet { Herbivore, Carnivore, Omnivore };
    }


    public interface IRun
    {
        void Run();
    }

}

Output

Humans run
Humans run
Humans run
Animals run
Animals run
Animals run

Here is the same code on Repl.it

Discussion

pic
Editor guide
Collapse
jeikabu profile image
jeikabu

Interfaces are key to C# (and many other languages).
Just thought I'd mention that showing the resulting console output would better illustrate what happens.

Collapse
kironroy profile image
Kiron Roy Author

Excellent, added the output

Collapse
wrldwzrd89 profile image
Eric Ahnell

Great interface illustration, though the "contract" analogy is somewhat imprecise, it's the best one I know of.

Collapse
kironroy profile image
Kiron Roy Author

An "Agreement" might work as well. Perhaps there can be no real analogy, because their are "loop holes" in Law, and only "patches and fixes" in programming, other aspects in programming are exact: both syntax errors and not implementing all the methods in a interface will lead to code that does not compile.

Code smells is another issue.