C# struct and interface

interface and struct boxing

struct is a value type while interface is a reference type. A cast from struct to an interface is a boxing.


using System;/*w ww .ja v  a2  s .  c om*/
interface Printable
{
    void print();
}

struct Rectangle : Printable
{
    public void print()
    {
        Console.WriteLine("rectangle");
    }
}


class Test
{
    static void Main()
    {

        Printable p = new Rectangle(); // boxing
        p.print();
    }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. When do we need a virtual interface implementation
Home »
  C# Tutorial »
    C# Types »
      C# Interface
C# Interfaces
C# interface implementation
C# Explicit interface
C# Interface inheritance
C# Indexer in an interface
C# Interface Properties
C# struct and interface
C# virtual interface