Thursday 6 February 2014

Basic Interfaces

Hi All,

Its been a while since I posted, so I thought I would get back to my blogging now I have a bit more free time. After pondering for many an hour over what interfaces do, it seems I have came to these conclusions:

- They do not implement any functionality
- They are more a scaffolding for which allows them to be easily swapped around with different classes
- They are using to overcome multiple inheritance issues.
- They use polymorphism Here is a basic example which will hopefully make sense: As you will see we have 2 interfaces setup Itest1 and Itest2.

These are called using instance.FirstMethod(); and instance.SecondMethod()




using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interfaces_Test
{
    class Program : Itest2
    {
        public static void Main()
        {
            Console.WriteLine("Hello Interfaces");
            var itest = new Program();
            Itest2 instance = itest;
            instance.FirstMethod();
            instance.SecondMethod();
           
        }

        void Itest1.FirstMethod()
        {
            Console.WriteLine("First Method Call");
            Console.ReadLine();
        }

        void Itest2.SecondMethod()
        {
            Console.WriteLine("Second Method Call");
            Console.ReadLine();
        }

    }

    internal interface Itest1
    {
        void FirstMethod();
    }

    internal interface Itest2 : Itest1
    {
        void SecondMethod();
      
    }
}

Ok so thats a couple of basics, obviously play around with these to get a better understanding in visual studio. The next step I would suggest is changing the code to something like this. You will notice I have removed the declaration for Itest1, this is not required as its already defined by the Itest2 interface.

NOTE: This code is designed to error, you will receive something along these lines, Error 1 'Interfaces_Test.Program' does not implement interface member 'Interfaces_Test.Itest1.FirstMethod()' c:\users\xxxxxx\documents\visual studio 2012\Projects\Interfaces Test\Interfaces Test\Program.cs 11 11 Interfaces Test

Hopefully you will see what is going on at this point and the penny will drop, so to speak, basically you cant implement Itest2 unless you have implemented Itest1.FirstMethod(), if you comment those lines back in (CTRL + K, CTRL + U key chords) then you will see the program will run successfully.



using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Interfaces_Test
{
    class Program : Itest2
    {
        public static void Main()
        {
            Console.WriteLine("Hello Interfaces");
            var itest = new Program();
            Itest2 instance = itest;
            instance.FirstMethod();
            instance.SecondMethod();
           
        }

        //void Itest1.FirstMethod()
        //{
        //    Console.WriteLine("First Method Call");
        //    Console.ReadLine();
        //}

        void Itest2.SecondMethod()
        {
            Console.WriteLine("Second Method Call");
            Console.ReadLine();
        }

    }

    internal interface Itest1
    {
        void FirstMethod();
    }

    internal interface Itest2 : Itest1
    {
        void SecondMethod();
      
    }
}

I am still learning a lot about interfaces myself, but I thought this could be of interest and hopefully help people who hate those mind boggling examples that give them a headache.

If anything is incorrect here then please let me know and I will correct it accordingly, thanks.

Good Luck :)

No comments: