次の方法で共有


No need for latent typing in C#

To follow up on my recent post, I came to the conclusion that I’m definitely not missing latent typing! At the end, the question is whether you want to have tightly or loosely coupled implementations. IMHO coupling at the level of a programming language has to be as tight and explicit as possible. I just want to make sure, that a successfully compiled program works the way I want it to work. If I’m responsible for a project, I want to enforce or prevent a certain semantical behavior: E.g. there is no need that the following code fragment compiles:

public class Gun

{

   public void Shoot() {;}

}

public class Camera

{

   public void Shoot(){;}

}

public class Container

{

   void CallShoot<T>(T t)

   {

      t.Shoot();

   }

  

   void Test()

   {

      Gun g = new Gun();

      Camera c = new Camera();

      CallShoot(g);

      CallShoot(c);

   }

}

The two classes have nothing in common, beside the accidentally chosen method name “Shoot”. I’m glad that this code doesn’t compile using C# generics!