C#泛型类与泛型方法 [英] C# Generic class vs Generic methods

查看:243
本文介绍了C#泛型类与泛型方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使类成为泛型而不是仅使方法泛型有什么好处?您想在哪种情况下使用泛型类?

What's the advantage of making a class generic as oppose to just make the method generic? Under which circumstance would you want to use a generic class?

推荐答案

以下是我的一些列表:

  • 使用通用类,您也可以具有通用属性.您可以将其与泛型类上的泛型方法一起使用.

  • With generic class, you can have generic properties as well. Which you can use together with generic methods you have on your generic class.

用于代码可重用性.您不想再次调用相同的泛型 不同的非泛型类上的方法,虽然可以执行此操作,但是 虽然不是一个好习惯.您想把所有相关的通用 通用类下的方法.

For code reusability. You don't want to call again the same generic method on different non-generic class, although you can do that, but not a good practice though. You want to put all your related generic methods under generic class.

这篇关于C#泛型类与泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆