在C#中,我可以调用buildError方法,如A.BuildError()而不提供泛型类型。 [英] In C#,Can I call buildError method Like A.BuildError() without giving generic type.

查看:40
本文介绍了在C#中,我可以调用buildError方法,如A.BuildError()而不提供泛型类型。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中



In Java

public class A<T>
 {
     private T response;


     private A(T response)
     {
         this.response = response;
     }

     public static <T> A<T> buildError()
     {
         A<T> trans = new A<T>(null);
         return trans;
     }

 }







我可以调用buildError方法像A.BuildError()没有给出泛型类型。



在C#




I can call buildError method Like A.BuildError() without giving generic type.

In C#

public class A<T>
 {
     private T response;


     private A(T response)
     {
         this.response = response;
     }

 
     public static A<T> buildError()
     {
         A<T> trans = new A<T>(null);
         return trans;
     }

 }





在C#中,我可以调用buildError方法,如A.BuildError( )没有给出泛型类型。



In C#,Can I call buildError method Like A.BuildError() without giving generic type.

推荐答案

否 - 由于没有参数或其他信息来识别可以返回的类型,您需要在调用时指定它方法。



但是这甚至都不会编译: null 不是一个对象而且没有类型!所以这一行:

No - Since there is no parameter or other information to identify the type that can be returned, you need to specify it when you call the method.

But that won't even compile: null is not an object and has no type! So this line:
A<t> trans = new A<t>(null);

只会给你一个编译错误。

will just give you a compilation error.


这篇关于在C#中,我可以调用buildError方法,如A.BuildError()而不提供泛型类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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