Trivial C# class with a generic parameter wouldn't compile for no apparent reason

后端 未结 7 881
夕颜
夕颜 2021-01-16 15:15

I want a generic function that would work with types that have Top, Bottom, Right and Rect read-only properties - I have

7条回答
  •  耶瑟儿~
    2021-01-16 15:36

    Generics is used for type safety. Now without any additional info about WhatType, how would the compiler know that the instance what that gets passed in will have a property Left?

    You need something like this

    public interface IFoo
    {
        int Left {get;}
    }
    

    and

    internal class MyTemplate where WhatType : IFoo {
    

提交回复
热议问题