泛型类型具有带有参数的构造函数

编程入门 行业动态 更新时间:2024-10-25 12:29:07
本文介绍了泛型类型具有带有参数的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 我对泛型类型有疑问.我试图制作一个泛型类,然后再制作一些带有带有参数的构造函数的类. 问题 -------- 当我尝试从泛型类调用方法时,发生了错误. 这是用于代码:

Hi all, I have a problem with generic type. I tried to make one generic class and then some classes that have constructor with parameter(s). Problem -------- When i''ll try to invoke method from generic class, the error was occurred. This is for the code :

public class Test { public Test(string a) { //todo.. } } public class Test2 { public Test(string a, int b) { //todo.. } } public Generic<T> where T : class, new() { public bool Save(T t){ //todo.. return blah... } } var result = new Generic<Test>().Save(blahhh);

错误:

Error :

must be a non-abstract type with a public parameterless constructor in order to use it as parameter T in the generic type

请帮帮我.. :(谢谢

Please help me.. :( Thanks

推荐答案

首先,您发布的代码有很多错误:此编译: First of all, your code that you published had a ton of errors: This compiles: public class Test { public Test(string a) { //todo.. } } public class Test2 { public Test2(string a, int b) { //todo.. } public Test2(); } public class Generic<T> where T : class, new() { public bool Save(T t) { //todo.. return true; } }

现在可以编译:

This will now compile:

var result = new Generic<test2>().Save(new Test2("dfd", 2));</test2>

更多推荐

泛型类型具有带有参数的构造函数

本文发布于:2023-11-02 15:05:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   参数   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!