Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

I have a piece of code that I don't understand why it works can someone help?

The piece is from his "14. How to program in C# - INTERFACES - Tutorial". As I said, I don't understand why it works, can someone please explain.

      for (int i = 0; i < inventory.Length; i++)   

 {

       IPartOfQuest questItem = inventory[i] as IPartOfQuest;

        if(questItem != null)

     {

          questItem.TurnIn();

        }

      }

Here is the whole program:

using System;

using System.Collections.Generic;


namespace FirstConsoleProject

{



  class MainClass

  {


    interface IItem

    { 

      string name { get; set; }

      int goldValue { get; set; }



      void Equip();


      void Sell();

    }


    interface IDamagable

    {

       int durability { get; set; }


      void TakeDamage(int _amount);


    }


    interface IPartOfQuest

    {

      void TurnIn();

    }


    class Sword : IItem, IDamagable, IPartOfQuest

    {

      public string name { get; set; }

      public int goldValue { get; set; }

      public int durability { get; set; }



      // CONSTRUCTOR

      public Sword(string _name)

      {

        name = _name;

        goldValue = 100;

        durability = 30;

      }


      public void Equip()

      {

        Console.WriteLine(name + " equipped.");

      }

      public void Sell()

      {

        Console.WriteLine(name + " sold for " + goldValue + "g.");

      }


      public void TakeDamage(int _dmg)

      {

        durability -= _dmg;

        Console.WriteLine(name + " took " + _dmg + " damage; It now has " + durability + " durability.");

      }


      public void TurnIn()

      {

        Console.WriteLine(name + " has been turned in.");

      }

    }


    class Axe : IItem, IDamagable

    {

      public string name { get; set; }

      public int goldValue { get; set; }

      public int durability { get; set; }



      // CONSTRUCTOR

      public Axe(string _name)

      {

        name = _name;

        goldValue = 70;

        durability = 50;

      }


      public void Equip()

      {

        Console.WriteLine(name + " equipped.");

      }

      public void Sell()

      {

        Console.WriteLine(name + " sold for " + goldValue + "g.");

      }

     

      public void TakeDamage(int _dmg)

      {

        durability -= _dmg;

        Console.WriteLine(name + " took " + _dmg + " damage; It now has " + durability + " durability.");

      }

    }

    public static void Main(string[] args)

    {

      Sword sword = new Sword("Sword Of Destiny");

      sword.Equip();

      sword.TakeDamage(20);

      sword.Sell();


      Console.WriteLine();

      Console.WriteLine();


      Axe axe = new Axe("Axemolyum");

      axe.Equip();

      axe.TakeDamage(10);

      axe.Sell();


      Console.WriteLine();

      // Create Inventory


      IItem[] inventory = new IItem[2];

      inventory[0] = sword;

      inventory[1] = axe;



      for (int i = 0; i < inventory.Length; i++)

      {

        IPartOfQuest questItem = inventory[i] as IPartOfQuest;

        if(questItem != null)

        {

          questItem.TurnIn();

        }

      }

    }

       

  }

}

Sign In or Register to comment.