Why use generic constraints in C#

前端 未结 7 1699
日久生厌
日久生厌 2020-11-29 05:48

I\'ve read an excellent article on MSDN regarding Generics in C#.

The question that popped in my head was - why should i be using generic constraints?

For ex

7条回答
  •  遥遥无期
    2020-11-29 06:16

    Type Safety. For example, suppose you're creating a container. You can pass in something to that container and retrieve it in the proper form without having to do any casts later by parameterizing the container. You're simply defining constraints on the types of things that you're willing to store in your container.

提交回复
热议问题