开发者

can I have an abstract base class with the key attribute being generic

开发者 https://www.devze.com 2022-12-29 16:14 出处:网络
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

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)?


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 ... )


Yes, you can do the following:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号