2011-04-02 3 views
1

Disons que Foo a 2 surcharges:Type Contraindre arg basé sur les surcharges existantes

void Foo(int[] array) { ... } 
void Foo(int[,] array) { ... } 

J'ai écrit une fonction fonction bar, qui appelle Foo, et je veux Bar pour soutenir à la fois int [] et int [,] arguments, j'ai donc ceci:

void Bar(int[] array) 
{ 
    // do some work here, and finally call Foo: 
    Foo(array); 
} 

void Bar(int[,] array) 
{ 
    // do some work here, and finally call Foo: 
    Foo(array); 
} 

Je veux supprimer la duplication de code en faisant un seul, la méthode de la barre générique. Pseudo:

void Bar<TArray>(TArray array) 
    where TArray is_a_type_accepted_by Foo 
{ 
    // do some work here, and finally call Foo: 
    Foo(array); 
} 

Est-ce possible?

Répondre

2

Non, une telle contrainte n'est pas possible en C#.

Questions connexes