Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Compile of Generic Methods

Tags:

c#

.net

generics

Currently working on a 2-way lookup association generic, sorted by TKey. At some point I hope to have access like the following:

public class Assoc<TKey, TValue>
{
     public TKey this[TValue value] { get; }
     public TValue this[TKey value] { get; }
}

But obviously when TKey == TValue this will fail. Out of curiosity, is there a conditional compile syntax to do this:

public class Assoc<TKey, TValue>
{
     [Condition(!(TKey is TValue))]
     public TKey this[TValue value] { get; }

     [Condition(!(TKey is TValue))]
     public TValue this[TKey value] { get; }

     public TKey Key(TValue value) { get; }

     public TValue Value(TKey value) { get; }
}
like image 714
Jake Avatar asked Aug 17 '12 01:08

Jake


1 Answers

No, there is no conditional compiltation based on Generic types.

Generics substitutions are performed at runtime, not compile time.

This is one of the differences between .NET generics and C++ templates.

Generics also don't have the concept of specialization that C++ templates have.

http://msdn.microsoft.com/en-us/library/c6cyy67b.aspx

like image 53
Andrew Shepherd Avatar answered Sep 20 '22 15:09

Andrew Shepherd