views:

66

answers:

2

Hi,

I want to create a re-usable library. I was going to use extension methods however I run into some issues in some cases for the client to have to specify in the calling method the types.

QUESTION - If I use an abstract base class as the basis, can I specify an attribute/property in the class to be generic (e.g. the key property might be an 'int' in one case, or a 'string' in another)?

+3  A: 

Yes.

public abstract class MyBase<T>
{
    public abstract T GetSomething();
}

public class MyConcreteType : MyBase<int>
{
    public override int GetSomething()
    {
         return 3;
    }
}

Or, what exactly do you mean ?
(Trying it out would have given you the answer faster then posting it on SO, I think ... )

Frederik Gheysels
+1  A: 

Yes, you can do the following:

public abstract class Class<T> 
{
    T value;
    T Prop { get; set;} 
}
Simon