[Mono-list] C# interfaces

Talbott Crowell tcrowell@thirdm.com
Mon, 10 Jun 2002 12:59:14 -0400


This is a multi-part message in MIME format.

------=_NextPart_000_0000_01C2107E.9F11C1E0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I am currently testing C# interfaces on Mono
and I am having difficulty compiling files that
use interfaces for more than just methods.  Mono
seems to fail when using an interface to access
events and indexers.

Is this currently not implemented by Mono?  If
not, are there plans to?

Also, in the .NET beta, a class that implemented
two interfaces with an identical method declaration
required explicit interface implementation.  The
final MS C# (csc.exe) compiler allows the class to 
compile without explicit interface implementation and
as a result both interfaces will execute a common
method.  Mono seems to be unable to reproduce this
behavior and instead mimics the beta behavior.
Is this intentional?

-Talbott

Talbott Crowell - tcrowell@thirdm.com
Principal Consultant
Third Millennium, LLP       

------=_NextPart_000_0000_01C2107E.9F11C1E0
Content-Type: text/plain;
	name="conflict.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="conflict.cs"

//mod14_interfaces\conflict.cs
//csc conflict.cs

using System;

public interface IDiagnostic
{
    bool IsWorking();
    void Start();
    void Stop();
} 
public interface IAutomobile
{
    void Accelerate();
    void Stop();
    void TurnWheel(float value);
}

public class MyCar: IAutomobile, IDiagnostic {
  public bool IsWorking() {
	// IDiagnostic implementation
	return true;
  }
  public void Start() {
	// IDiagnostic implementation
  }
  public void Stop() { 
	// IDiagnostic or IAutomobile implementation
	Console.WriteLine("stop the automobile or diagnostics?");
  }
  public void Accelerate() {
	// IAutomobile implementation
  }
  public void TurnWheel(float value) {
	// IAutomobile implementation
  }
}

class TestConflict 
{
    static void Main()
    {
        MyCar car1 = new MyCar();
        car1.Stop();
        
        IDiagnostic car2 = new MyCar();
        car2.Stop();

        IAutomobile car3 = new MyCar();
        car3.Stop();
    }
}
------=_NextPart_000_0000_01C2107E.9F11C1E0
Content-Type: text/plain;
	name="interface_indexer.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="interface_indexer.cs"

//mod14_interfaces\interface_indexer.cs
//csc interface_indexer.cs

using System.Collections;

interface IEmployeeGroup {
   Employee this[int index] {get;}
   void Add(Employee emp);
   int Count { get; }
}

public class Staff: IEmployeeGroup {  
   private ArrayList employees = new ArrayList();
   public Employee this[int index] {
      get { return (Employee) employees[index]; }
   }
   public void Add(Employee emp) {
      employees.Add(emp);
   }
   public int Count {
      get { return employees.Count; }
   }
}

public class Employee {
    public string FirstName;
    public Employee(string firstName) {
        FirstName = firstName;
    }
}

class InterfaceIndexer {
   static void Main() {
      IEmployeeGroup group = new Staff();
      group.Add(new Employee("Fred"));
      group.Add(new Employee("Barney"));
      group.Add(new Employee("Wilma"));
      
      int qty = group.Count;
      for(int i = 0; i < qty; i++) {
         System.Console.WriteLine(group[i].FirstName);
      }
   }
}

------=_NextPart_000_0000_01C2107E.9F11C1E0
Content-Type: text/plain;
	name="interface_events.cs"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="interface_events.cs"

//mod14_interfaces\interface_events.cs
//csc interface_events.cs

using System;

interface IAutomobile
{
  event System.EventHandler LowOnGas;
  void Accelerate();
  void Stop();
}

public class Car: IAutomobile {
   private double gas = 20.0d;
   private const double fuelCapacity = 20.0d;
   
   public event EventHandler LowOnGas;

   public void Accelerate() {
      gas = gas - 1.1d;
      if (gas <= 0.0d) {
          Console.Error.WriteLine("out of gas!");
          throw new Exception();
      }
      if (gas < (fuelCapacity * 0.10d)) {
         if (LowOnGas != null) {
            LowOnGas(this, new EventArgs());
         }
      }
      Console.WriteLine("Going, gas = " + gas);
   }
   public void Stop() {
      Console.WriteLine("Stopped.  gas = " + gas);
   }
}

class Test {
   static bool timeToStop;
   static void Main() {
      IAutomobile auto = new Car();
      auto.LowOnGas += new EventHandler(runningLow);
      while(!timeToStop) {
         auto.Accelerate();
      }
      auto.Stop();
   }
   static void runningLow(object sender, EventArgs e) {
      timeToStop = true;
   }
}

------=_NextPart_000_0000_01C2107E.9F11C1E0--